【发布时间】:2013-11-23 22:49:48
【问题描述】:
我正在尝试进行网络抓取并使用以下代码:
import mechanize
from bs4 import BeautifulSoup
url = "http://www.thehindu.com/archive/web/2010/06/19/"
br = mechanize.Browser()
htmltext = br.open(url).read()
link_dictionary = {}
soup = BeautifulSoup(htmltext)
for tag_li in soup.findAll('li', attrs={"data-section":"Chennai"}):
for link in tag_li.findAll('a'):
link_dictionary[link.string] = link.get('href')
print link_dictionary[link.string]
urlnew = link_dictionary[link.string]
brnew = mechanize.Browser()
htmltextnew = brnew.open(urlnew).read()
articletext = ""
soupnew = BeautifulSoup(htmltextnew)
for tag in soupnew.findAll('p'):
articletext += tag.text
print articletext
我无法通过使用它来获得任何打印值。但是在使用 attrs={"data-section":"Business"} 而不是 attrs={"data-section":"Chennai"} 时,我能够获得所需的输出。有人可以帮我吗?
【问题讨论】:
标签: python python-2.7 web-scraping beautifulsoup web-crawler