【发布时间】:2015-03-02 00:23:03
【问题描述】:
我有这个代码,但它不工作。我想使用 urllib2 来遍历 url 列表。在打开每个 url 时,BeautifulSoup 会找到一个类并提取该文本。如果列表中有无效的 url,程序就会停止。如果有任何错误,我只想将“错误”作为文本,并让程序继续到下一个 url。有什么想法吗?
for url in url_list:
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
text = soup.find_all(class_='ProfileHeaderCard-locationText u-dir')
if text is not None:
for t in text:
text2 = t.get_text().encode('utf-8')
else:
text2 = 'error'
【问题讨论】:
标签: python http beautifulsoup urllib2