I have been trying to automate login into stackoverflow to learn web scraping. First I tried scrapy, from which I did not get that lucky using following code.
import scrapy
from scrapy.utils.response import open_in_browser
class QuoteSpider(scrapy.Spider):
name = 'stackoverflow'
start_urls = ['https://stackoverflow.com/users/login']
def parse(self, response):
token = response.xpath('.//*[@name="fkey"]/@value').extract_first()
yield scrapy.FormRequest('https://stackoverflow.com/users/login?ssrc=head&returnurl=https://stackoverflow.com/',
formdata = {
'fkey': token,
"ssrc": "head",
'username': "example@gmail.com",
'password': 'example123',
'oauth_version':'',
'oauth_server':''
},callback=self.startscraper)
def startscraper(self,response):
yield scrapy.Request('https://stackoverflow.com/users/12454709/gopal-kisi',callback=self.verifylogin)
def verifylogin(self,response):
open_in_browser(response)
So, I tried selenium later, I successfully login to stackoverflow using following code.
from selenium import webdriver
import pandas as pd
import time
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2f")
time.sleep(2)
username = driver.find_element_by_xpath("//*[@id='email']")
username.clear()
username.send_keys("example@gmail.com")
time.sleep(5)
password = driver.find_element_by_xpath("//*[@id='password']")
password.clear()
password.send_keys("example123")
time.sleep(0.5)
driver.find_element_by_xpath("//*[@id='submit-button']").click()
driver.close()
I know selenim and scrapy are two different method. Now, for scraping, I found scrapy is much more easier to process and save data than selenium and also it uses headless browsing, just like I needed.
So, is there any way to solve login problem in scrapy. Or, can I merge selenium with scrapy, so that, I cound login with selenium and then remaining work can be done by scrapy?
from scrapy import FormRequest
url = "https://stackoverflow.com/users/login"
fetch(url)
req = FormRequest.from_response(
response,
formid='login-form',
formdata={'email': 'test@test.com',
'password': 'testpw'},
clickdata={'id': 'submit-button'},
)
fetch(req)