我正在使用一个CrawlSpider,它递归地跟随链接,使用链接提取调用下一个页面,如下所示:
rules = (Rule(LinkExtractor(
allow=(),\
restrict_xpaths=('//a[contains(.,"anextpage")]')),\
callback='parse_method',\
follow=True),
)
我已经应用了这个策略来递归爬行不同的网站,只要html标签中有文本,比如<a href="somelink">sometext</a>
,一切都运行得很好。
我现在正在尝试抓取一个网站,它有一个
<div class="bui-pagination__item bui-pagination__next-arrow"> <a class="pagenext" href="/url.html" aria-label="Pagina successiva"> <svg class="bk-icon -iconset-navarrow_right bui-pagination__icon" height="18" role="presentation" width="18" viewBox="0 0 128 128"> <path d="M54.3 96a4 4 0 0 1-2.8-6.8L76.7 64 51.5 38.8a4 4 0 0 1 5.7-5.6L88 64 57.2 94.8a4 4 0 0 1-2.9 1.2z"></path> </svg> </a> </div>
作为“下一步”按钮,而不是简单的文本,我的LinkExtractor规则似乎不再适用,爬行器在第一页后停止。
我尝试寻找svg元素,但似乎没有触发提取:
restrict_xpaths=('//a[contains(.,name()=svg) and contains(@class,"nextpageclass")]'))
我是不是遗漏了什么?
发布于 2020-08-11 12:45:18
这很可能是因为网站使用的是javascript。您可能需要使用Splash来模拟单击以导航和返回预先呈现的网站。这是一个很好的起点:
https://docs.scrapy.org/en/latest/topics/dynamic-content.html
https://stackoverflow.com/questions/55919187
复制相似问题