使用scrapy导航到ScienceDirect的下一页

2024-10-06 11:17:58 发布

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

使用scray,如何从sciencedirect.com生成的任何结果页导航到“nextpage”链接?在

nextpage链接是输入元素:

<div class="paginationBar">
<span style="color:#A4A4A4;" aria-disabled="true" alt="Previous Page" title="Previous Page"><< Previous</span>
<span class="pageText">Page 1 of 20462</span>
<input class="nextPrev" type="submit" title="Next Page" alt="Next Page" name="bottomNext" onmouseout="this. className='nextPrev'" onmouseover="this.className='nextPrevHov'" value="Next >>">
</div>

并且存在一些javascript,但我不知道如何使用它:(


Tags: divtitle链接pagethisaltclassnext
1条回答
网友
1楼 · 发布于 2024-10-06 11:17:58

答案很简单:不涉及JavaScript。在

如果你看一下这个站点,你会发现链接Next >>是一个input字段,submitform。在

当查看form本身时,您可以看到它向站点发送get请求。这个请求的input字段可以聚集在一起,然后yield一个新的{}与Scrapy一起刮下一个站点。在

例如:

form = response.xpath('//form[@name="Tag"]')[0]
url = 'http://www.sciencedirect.com/science/?'
for inp in form.xpath('.//input[@type="hidden"]'):
    url += inp.xpath('./@name').extract()[0]+'='+inp.xpath('./@value').extract()[0]+'&'
url += 'bottomNext=Next+%3E%3E&resultsPerPage=25'
yield Request(url)

当然,需要一些错误处理(例如,在1000个结果之后,您无法查看更多结果,因此您将得到一个没有form的错误站点)。在

相关问题 更多 >