Python 使用scrapy登录不工作

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.

我已经为登录到一个站点写了一些零碎的代码。首先,我尝试了一个网站。它工作得很好。但后来我更改了url并尝试了其他网站。它不适用于该站点。我使用了相同的代码,没有任何更改。有什么问题吗

        # -*- 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

  • 登录获取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

  • 登录获取url不包括在
    允许的\u域中

    • 允许的\u域=['http://collegekart.in/login']
    • 登录获取url:
      http://collegekart.in/access/.......
建议
  • 使用
    Chrome's Inspector>Network
    查看执行登录操作时发出的实际请求

  • 查看此官方教程(PDF版本):


在这里相应地更改响应url,这将解决问题。

在这里相应地更改响应url,这将解决问题。

如果一个脚本适用于一个站点,并不意味着它将适用于另一个站点如果一个脚本适用于一个站点,并不意味着它将适用于另一个站点我是个新手。我现在正在学习。谢谢again@user8931234,如果它解决了你的问题,一定要通过勾选答案旁边的复选标记来接受它作为答案。太好了!它奏效了。我是个新手。我现在正在学习。谢谢again@user8931234,如果它解决了您的问题,请确保通过勾选复选框接受它作为答案答案旁边是k。
# -*- 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")