Issue
I've been trying to login to a website with scrapy, but it doesn't work properly
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"))
Solution
For the email and password you'll need to take the value in name
attribute. You can check if the login was successful the same way that I did, but maybe change the strings.
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 = '[email protected]'
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"))
Answered By - SuperUser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.