目前,我正在尝试爬行一个页面,它需要有一个会话指示,以便让我获得一些信息。为此,我使用Scrapy库创建了一个简单的Python程序,但我不能完全确定它是否被正确开发,因为我不知道如何调试它(或者这是否可能),现在我没有得到任何结果。
目前,我的代码如下所示:
import scrapy
from scrapy import FormRequest
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from strava_crawler.items import StravaCrawlerItem
from scrapy.exceptions import CloseSpider
from scrapy.utils.response import open_in_browser
class stravaSpider(CrawlSpider):
name = 'stravaSpider'
item_count = 0
allowed_domain = ['https://www.strava.com']
start_urls = ['https://www.strava.com/login']
def parse(self, response):
token = response.xpath("//meta[@name='csrf-token']/@content").extract_first()
print(token)
yield FormRequest.from_response(response, formdata={
'username': 'xxxx@xxx.com',
'password': 'xxxxx',
'authenticity_token': token
}, callback=self.start_scraping)
def start_scraping(self, response):
sc_item = StravaCrawlerItem()
sc_item['titulo'] = response.xpath('//div[@class="athlete-name"]/text()').extract()
self.item_count += 1
if self.item_count == 1:
raise CloseSpider('item_exceeded')
yield sc_item代码看起来很简单,正如你所看到的,但我的问题来自其中的两点。第一个是在:
token = response.xpath("//meta[@name='csrf-token']/@content").extract_first()我真的不太确定,我看了页面上的html,我找到了:

我的第二个问题是来自自己页面的网络头,它看起来像这样:

这让我对我的FormRequest.from_response产生了怀疑,我已经在自己的from_response中看到了使用csrf-token的解决方案,但我也尝试过,也没有得到任何响应。这是我在终端上得到的结果,我想对于这个问题可能会很有趣。

你是否发现程序的代码或概念上有什么问题?
编辑:经过几次更改后,我得到了一个新的输出,它看起来像一个重定向循环imo,其中我从仪表板重定向到登录。我打印response.body,它是登录页面的HTML码。

EDIT2:我在Ubuntu上运行了代码(我在windows上),并且工作得很好。因此,您可以随意使用它作为scrapy日志记录的示例。
发布于 2020-10-06 20:29:23
中进行测试: shell
>>> from scrapy import FormRequest
>>> req = FormRequest.from_response(response, formdata={
... 'username': 'xxxx@xxx.com',
... 'password': 'xxxxx',
... })
>>> req.body
b'utf8=%E2%9C%93&authenticity_token=L4mSH2wLcNAiLcR7yqCb%2BEdaNyPyJqU%2BbbT1ct9wQGWPnqstXVM5bWX1tmIPq62qpp4FpHdsjazlruVe%2Ba0xpg%3D%3D&plan=&email=&username=xxxx%40xxx.com&password=xxxxx'我会试着这样测试它:
def parse(self, response):
yield FormRequest.from_response(
response,
formid="login_form",
clickdata={'type': 'submit'},
formdata={
'email': 'xxxx@xxx.com',
'password': 'xxxxx',
},
callback=self.start_scraping)https://stackoverflow.com/questions/64215653
复制相似问题