【发布时间】:2016-08-02 12:19:45
【问题描述】:
我已经编写了在网页中搜索一些随机文本的代码块。该网页有多个标签,我正在使用 selenium 进行导航。这是我要查找的文本未在特定页面中修复的问题。文本可以位于网页中的任何选项卡中。如果未找到文本,则会引发异常。如果引发异常,则应转到下一个选项卡进行搜索。我在处理异常时遇到了困难。
下面是我正在尝试的代码。
import requests
from bs4 import BeautifulSoup
import re
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.yxx.com/71463001")
a = driver.page_source
soup = BeautifulSoup(a, "html.parser")
try:
head = soup.find_all("div", {"style":"overflow:hidden;max-height:25px"})
head_str = str(head)
z = re.search('B00.{7}', head_str).group(0)
print z
print 'header'
except AttributeError:
g_info = soup.find_all("div", {"id":"details_readonly"})
g_info1=str(g_info)
x = re.search('B00.{7}', g_info1).group(0)
print x
print 'description'
except AttributeError:
corre = driver.find_element_by_id("tab_correspondence")
corre.click()
corr_g_info = soup.find_all("table", {"id" : "correspondence_view"})
corr_g_info1=str(corr_g_info)
print corr_g_info
y = re.search('B00.{7}', corr_g_info1).group(0)
print y
print 'correspondance'
当我运行这段代码时,我得到一个
error Traceback (most recent call last):
File "C:\Python27\BS.py", line 21, in <module>
x = re.search('B00.{7}', g_info1).group(0)
AttributeError: 'NoneType' object has no attribute 'group'
【问题讨论】:
标签: python-2.7 selenium-webdriver exception-handling beautifulsoup