我现在完全被代码困住了。首先,我尝试从大众传媒的存档页面中检索所有URL。这是我被击中的第一步。一个特定日期的url如下所示:http://www.volkskrant.nl/archief/detail/01012016
最后的数字反映了日期,而且每页都是一样的,所以我想出了一个主意,把日期(DD)、月份(MM)和年份(2016)串起来。你知道吗
下一步,我会找到这些页面的链接,引导我找到一个页面,上面有更多我最终想要得到的文章的链接。我知道如何得到其中一个日期的网址,但不是所有的。最后,我需要刮去所有的文本页,我也可以做一页。另一个问题是,我还需要检索下一页上的所有文章,但我不知道如何在代码中实现这一点。你知道吗
基本上,我在遍历链接时遇到了很大的问题,特别是在使用字符串时。希望有人能帮我解决这个问题。你知道吗
The code looks as following atm:
> Scraping archive of Volkskrant
month=['01','02'.. etc]
day =['01','02','03'.. etc]
year=['2016']
for x in month:
for y in day:
next_date= 'http://www.volkskrant.nl/archief/detail/'+str(y)+str(x)+str(year)
> getting links of one single date
req=request.Request('http://www.volkskrant.nl/archief/detail/01012016', headers={'User-Agent':"Mozilla/5.0"})
archive=request.urlopen(req).read()
archive=archive.decode(encoding="utf-8",errors="ignore").replace("\n"," ").replace("\t"," ")
for link in archive:
links=re.findall(r'<article class="article article--extended".*?</article>', archive)
url1= ''.join(map(str,links))
for item in url1:
urls= re.findall(r'href=[\'"]?([^\'">]+)', url1)
>go to next page and retrieve all links there
nextpage=re.findall(r'<span class="pagination__item">.*?</span>', archive)
nextp= ''.join(map(str,nextpage))
for item in nextp:
next= re.findall (r'href=[\'"]?([^\'">]+)', nextp)[:1]
> retrieving one article and scrape content
req=request.Request('http://www.volkskrant.nl/politiek/pechtold-wil-d66-blijven-leiden~a4283833/', headers={'User-Agent':"Mozilla/5.0"})
tekst=request.urlopen(req).read()
tekst=tekst.decode(encoding="utf-8",errors="ignore").replace("\n"," ").replace("\t"," ")
> scraping the introduction with Xpath as the regex was not applicable
tree=html.fromstring(request.urlopen(req).read().decode(encoding="utf-8", errors="ignore"))
artikel3=tree.xpath('//*[@itemprop="description"]/text()')
... etc
这将为您提供所有链接,包括分页和从1月1日到现在的每一天:
要获取文本,只需分析每个链接:
对于一些结果:
相关问题 更多 >
编程相关推荐