网络刮痧形成新闻数据库

时间:2013-11-11 20:33:15

标签: python python-2.7 python-3.x beautifulsoup

我正在为不同的新闻媒体创建一个网络刮刀。我试图为The Hindu报纸创建一个。

我想从其档案中提到的各种链接中获取新闻。假设我想在第二天提到的链接上获取新闻:http://www.thehindu.com/archive/web/2010/06/19/即2010年6月19日。

现在我写了以下几行代码:

import mechanize
from bs4 import BeautifulSoup

url = "http://www.thehindu.com/archive/web/2010/06/19/"

br =  mechanize.Browser()
htmltext = br.open(url).read()

articletext = ""
soup = BeautifulSoup(htmltext)
for tag in soup.findAll('li', attrs={"data-section":"Business"}):
    articletext += tag.contents[0]
print articletext

但我无法获得所需的结果。我基本上卡住了。有人可以帮我解决一下吗?

2 个答案:

答案 0 :(得分:5)

请尝试以下代码:

import mechanize
from bs4 import BeautifulSoup

url = "http://www.thehindu.com/archive/web/2010/06/19/"

br =  mechanize.Browser()
htmltext = br.open(url).read()

articletext = ""
for tag_li in soup.findAll('li', attrs={"data-section":"Op-Ed"}):
    for link in tag_li.findAll('a'):
        urlnew = urlnew = link.get('href')
        brnew =  mechanize.Browser()
        htmltextnew = brnew.open(urlnew).read()            
        articletext = ""
        soupnew = BeautifulSoup(htmltextnew)
        for tag in soupnew.findAll('p'):
            articletext += tag.text
        print re.sub('\s+', ' ', articletext, flags=re.M)

driver.close()

对于re,您可能需要导入re模块。

答案 1 :(得分:1)

我建议你查看Scrapy。尝试使用您的参数进行教程并进行实验。他们拥有比机械化模块更加发达的网络爬行基础设施。