网络抓取形成新闻数据库

2024-09-25 16:32:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为不同的新闻机构创建一个网页刮板。我试图为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

但我无法得到所需的结果。我基本上被卡住了。有人能帮我解决吗?在


Tags: brimportcomwebhttpurlwww新闻
2条回答

我建议你退房。用你的参数试试他们的教程,然后用它来做实验。他们有一个比机械化模块更发达的网络爬行基础设施。在

请尝试以下代码:

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模块。在

相关问题 更多 >