Login to stackoverflow using selenium is working but using scrapy python is not.How can I login with headless browsing?

后端 未结 2 1846
野性不改
野性不改 2021-01-22 11:11

I have been trying to automate login into stackoverflow to learn web scrapping. First I tried scrapy, from which I did not get that lucky using following code.

im         


        
2条回答
  •  醉酒成梦
    2021-01-22 11:16

    In addition to the way by Wim Hermans, you can also POST https://stackoverflow.com/users/login with the following parameters:

    • email: your email
    • password: your password
    • fkey

    Here's an example:

    import requests
    import getpass
    from pyquery import PyQuery
    
    # Fetch the fkey
    login_page = requests.get('https://stackoverflow.com/users/login').text
    pq = PyQuery(login_page)
    fkey = pq('input[name="fkey"]').val()
    
    # Prompt for email and password
    email = input("Email: ")
    password = getpass.getpass()
    
    # Login
    requests.post(
        'https://stackoverflow.com/users/login',
        data = {
            'email': email,
            'password': password,
            'fkey': fkey
        })
    

提交回复
热议问题