【发布时间】:2016-06-09 07:49:45
【问题描述】:
我正在使用 Python-2.7 和 BeautifulSoup
参考我的this 问题,我试图从名称几乎相似的 div 标签中获取内容。因此,我需要严格检查 div 标签的类名。
以下是我的代码-
list = ['Link1','Link2','Link3','Link4',....etc]
for i in list:
mech = Browser()
mech.set_handle_robots(False)
mech.set_handle_equiv(False)
hadr = {'User-Agent':'Agent'}
req = urllib2.Request(i,headers=hadr)
try:
pan = urllib2.urlopen(req)
soup = BeautifulSoup(pan, "lxml")
tag1 = soup.select("div[class=profile-container abc-profile-container]")
print "TAG_1",tag1
tag2 = soup.select("div[class=profile-container]")
print "TAG_2",tag2
except Exception as e:
print e
print(type(e))
我想进一步说明的是列表中的任何随机链接都包含 tag1 的 div 类,但其输出为空白。
我希望所有具有("div[class=profile-container abc-profile-container]") 的链接都应该接受 tag1 并相应地工作,而不是给出一个空白列表作为输出。
【问题讨论】:
-
既然您最初说的是
Guidance / Help in any form is appreciated,我建议您查看traceback.print_exc而不是print(e),print(type(e)),它的信息量非常丰富。 -
您好,非常感谢,但有人建议我编辑它,因此我这样做了。虽然我一定会试试这个
-
嗯,是的,它吸引了像我这样的主题外的 cmet。 :P
标签: python python-2.7 css-selectors beautifulsoup web-crawler