我有一个scrapy sider,可以成功地登录到ancestry.com。然后,我使用经过身份验证的会话返回一个新链接,并可以成功地抓取新链接的第一页。当我尝试转到第二页时,出现问题。我收到一条302重定向调试消息,并且url:https://secure.ancestry.com/error/reqvalidation.aspx?aspxerrorpath=http%3a%2f%2fsearch.ancestry.com%2ferror%2fPageNotFound&msg=&ti=0>。
我遵循了文档,并按照这里的一些建议走到了这一步。每个页面都需要一个会话令牌吗?如果是这样,我是如何做到这一点的?
import scrapy
from scrapy import Request
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from scrapy.http import FormRequest
from scrapy.loader import ItemLoader
from ..items import AncItem
class AncestrySpider(CrawlSpider):
name = 'ancestry'
def start_requests(self):
return[
FormRequest(
'https://www.ancestry.com/account/signin?returnUrl=https%3A%2F%2Fwww.ancestry.com',
formdata={"username": "foo", "password": "bar"},
callback=self.after_login
)
]
def after_login(self, response):
if "authentication failed".encode() in response.body:
self.log("Login failed", level=log.ERROR)
return
else:
return Request(url='https://www.ancestry.com/search/collections/nypl/?name=_Wang&count=50&name_x=_1',
callback=self.parse)
def parse(self, response):
all_products = response.xpath("//tr[@class='tblrow record']")
for product in all_products:
loader = ItemLoader(item=AncItem(), selector=product, response=response)
loader.add_css('Name', '.srchHit')
loader.add_css('Arrival_Date', 'td:nth-child(3)')
loader.add_css('Birth_Year', 'td:nth-child(4)')
loader.add_css('Port_of_Departure', 'td:nth-child(5)')
loader.add_css('Ethnicity_Nationality', 'td:nth-child(6)')
loader.add_css('Ship_Name', 'td:nth-child(7)')
yield loader.load_item()
next_page = response.xpath('//a[@class="ancBtn sml green icon iconArrowRight"]').extract_first()
if next_page is not None:
next_page_link = response.urljoin(next_page)
yield scrapy.Request( url=next_page_link, callback=self.parse)我试着添加一些请求头信息。我尝试将cookie信息添加到请求标头,但不起作用。我只尝试使用POST包中列出的用户代理。
现在我只能得到50个结果。在抓取所有页面后,我应该得到数百个。
发布于 2019-05-03 23:01:21
找到了解决方案。这与网站的身份验证无关。我需要找到一种不同的方法来实现分页。我求助于使用页面url进行分页,而不是使用“下一页”按钮链接。
https://stackoverflow.com/questions/55940238
复制相似问题