Python 使用scrapy登录不工作
我已经为登录到一个站点写了一些零碎的代码。首先,我尝试了一个网站。它工作得很好。但后来我更改了url并尝试了其他网站。它不适用于该站点。我使用了相同的代码,没有任何更改。有什么问题吗Python 使用scrapy登录不工作,python,web-scraping,scrapy,scrapy-spider,Python,Web Scraping,Scrapy,Scrapy Spider,我已经为登录到一个站点写了一些零碎的代码。首先,我尝试了一个网站。它工作得很好。但后来我更改了url并尝试了其他网站。它不适用于该站点。我使用了相同的代码,没有任何更改。有什么问题吗 # -*- coding: utf-8 -*- import scrapy from scrapy.http import FormRequest from scrapy.utils.response import open_in_browser class QuoteSpider(scrapy.
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser
class QuoteSpider(scrapy.Spider):
name = 'Quote'
allowed_domains = ["quotes.toscrape.com"]
start_urls = (
'http://quotes.toscrape.com/login',
)
def parse(self, response):
token=response.xpath('//input[@name="csrf_token"]/@value').extract_first()
return FormRequest.from_response(response,formdata={'csrf_token':token,'password':'foo','username':'foo'},callback=self.scape_home_page)
def scape_home_page(self, response):
open_in_browser(response)
这很有效
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser
class BucketsSpider(scrapy.Spider):
name = 'buckets'
allowed_domains = ['http://collegekart.in/login']
start_urls = ['http://collegekart.in/login/']
def parse(self, response):
token=response.xpath('//meta[@name="csrf-token"]/@content').extract_first()
print(token)
return FormRequest.from_response(response,formdata={'csrf-token':token,'password':'*********','username':'**************'},callback=self.scape_home_page)
def scape_home_page(self, response):
open_in_browser(response)
print("yes")
这是行不通的。请帮忙解决这个问题。怎么了
- `…来自_响应(响应)。。。。。。。。
- 如果您检查
,它将为您提供response.url
而不是http://collegekart.in/login/
http://collegekart.in/
- 如果您检查
允许的\u域=
['http://collegekart.in/login“]
的登录获取请求不在您的collegekart.in/
允许的\u域中
- 如果未替换
中的响应
变量:url
- scrapy会将您的请求发送到错误的url:
http://collegekart.in/
登录
访问/尝试登录?utf8=%E2%9C%93&username=zerqqr1%40iydhp.com&password=hanfenghanfeng
- 这是正确的url:
http://collegekart.in/access/attempt_login?utf8=%E2%9C%93&username=zerqqr1%40iydhp.com&password=hanfenghanfeng
- scrapy会将您的请求发送到错误的url:
- 登录获取url不包括在
允许的\u域中
允许的\u域=['http://collegekart.in/login']
- 登录获取url:
http://collegekart.in/access/.......
- 使用
查看执行登录操作时发出的实际请求Chrome's Inspector>Network
- 查看此官方教程(PDF版本):
- `…来自_响应(响应)。。。。。。。。
- 如果您检查
,它将为您提供response.url
而不是http://collegekart.in/login/
http://collegekart.in/
- 如果您检查
允许的\u域=
['http://collegekart.in/login“]
的登录获取请求不在您的collegekart.in/
允许的\u域中
- 如果未替换
中的响应
变量:url
- scrapy会将您的请求发送到错误的url:
http://collegekart.in/
登录
访问/尝试登录?utf8=%E2%9C%93&username=zerqqr1%40iydhp.com&password=hanfenghanfeng
- 这是正确的url:
http://collegekart.in/access/attempt_login?utf8=%E2%9C%93&username=zerqqr1%40iydhp.com&password=hanfenghanfeng
- scrapy会将您的请求发送到错误的url:
- 登录获取url不包括在
允许的\u域中
允许的\u域=['http://collegekart.in/login']
- 登录获取url:
http://collegekart.in/access/.......
- 使用
查看执行登录操作时发出的实际请求Chrome's Inspector>Network
- 查看此官方教程(PDF版本):
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser
class BucketsSpider(scrapy.Spider):
name = 'buckets'
allowed_domains = ['collegekart.in']
start_urls = ['http://collegekart.in/login/']
def parse(self, response):
token=response.xpath('//meta[@name="csrf-token"]/@content').extract_first()
print(token)
response = response.replace(url='http://collegekart.in/')
return FormRequest.from_response(response,formdata={'csrf-token':token, 'password':'hanfenghanfeng','username':'zerqqr1@iydhp.com'},callback=self.scape_home_page)
def scape_home_page(self, response):
open_in_browser(response)
print("yes")