如何使用Scrapy登录本网站

atmip9wb  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(151)

我一直试图登录到一个网站与scrapy,但它不能正常工作

import scrapy

class BasicSpider(scrapy.Spider):
    name = 'basic'
    # allowed_domains = ['login']
    start_urls = ['https://seller.digikala.com/account/login/?_back=https://seller.digikala.com/sellerinvoice/']

def parse(self, response):
    # login = {}
    return scrapy.FormRequest.from_response(
        response,
        formdata={'email': 'email', 'password': 'pass'},
        callback=self.parse_item
    )

def parse_item(self, response):
    if response.body != None:
        print(response.xpath("/html/body/div/aside[1]/div/header/a/img"))
56lgkhnf

56lgkhnf1#

对于电子邮件和密码,你需要取name属性中的值。你可以用和我一样的方法检查登录是否成功,但是可能需要更改字符串。

import scrapy

class BasicSpider(scrapy.Spider):
    name = 'basic'
    # allowed_domains = ['login']
    start_urls = ['https://seller.digikala.com/account/login/?_back=https://seller.digikala.com/sellerinvoice/']

    email = 'yourEmailAddress@some.thing'
    password = 'yourPassword'

    def parse(self, response):
        # login = {}
        return scrapy.FormRequest.from_response(
            response,
            formdata={'login[email]': self.email, 'login[password]': self.password},
            callback=self.parse_item
        )

    def parse_item(self, response):
        if 'آدرس ایمیل صحیح نمی‌باشد' in response.text:
            self.log('Email address is incorrect')
            return
        elif 'نام کاربری یا رمز عبور اشتباه است لطفا دوباره تلاش نمایید' in response.text:
            self.log('Username or password is incorrect')
            return
        print(response.xpath("/html/body/div/aside[1]/div/header/a/img"))

相关问题