Python 2.7 Beauty soup/Python中的异常处理
我编写了代码块,用于搜索网页中的一些随机文本。该网页有多个选项卡,我正在使用selenium进行导航。这里的问题是,我试图找到的文本在特定页面中没有得到修复。文本可以位于网页中的任何选项卡中。如果未找到文本,将引发异常。如果引发异常,则应转到下一个选项卡进行搜索。我在处理异常方面遇到了困难 下面是我正在尝试的代码Python 2.7 Beauty soup/Python中的异常处理,python-2.7,selenium-webdriver,exception-handling,beautifulsoup,Python 2.7,Selenium Webdriver,Exception Handling,Beautifulsoup,我编写了代码块,用于搜索网页中的一些随机文本。该网页有多个选项卡,我正在使用selenium进行导航。这里的问题是,我试图找到的文本在特定页面中没有得到修复。文本可以位于网页中的任何选项卡中。如果未找到文本,将引发异常。如果引发异常,则应转到下一个选项卡进行搜索。我在处理异常方面遇到了困难 下面是我正在尝试的代码 import requests from bs4 import BeautifulSoup import re from selenium import webdriver drive
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'
错误回溯(最近一次调用上次):
文件“C:\Python27\BS.py”,第21行,在
x=re.search('B00.{7}',g_info1).group(0)
AttributeError:“非类型”对象没有属性“组”
您收到该错误是因为您正在调用不包含任何内容的re.search对象上的组。当我运行你的代码时,它失败了,因为你试图连接的页面当前没有打开
至于你的except
没有捕捉到它的原因:你错误地为一个只写了两个except
s,试试看。try
只会捕获第一个之前代码的任何AttributeError
s,除了
通过将第19行更改为x=re.search('B00.{7}',g_info1)
,代码运行并再次返回None
和description
,因为页面当前未打开
或者,要实现我认为您要实现的目标,可以选择嵌套try
/,但
除外:
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:
try:
g_info = soup.find_all("div", {"id":"details_readonly"})
g_info1=str(g_info)
x = re.search('B00.{7}', g_info1)
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'
当然,此代码当前会抛出一个namererror
,因为站点上没有定义corr\u g\u info
变量的信息。谢谢,这是有效的。添加嵌套的try-catch有效。