简体   繁体   中英

Cannot login using Python Requests module

I am trying to do some web scraping using python with Requests module in a session, but I always failed to login, and here is the site. I know there are a lot of similar questions but I have really tried most of the solutions. So I guess I have to post a new one. This is my code so far. This site contains a csrf-token in the requests header and I have included it already.

from bs4 import BeautifulSoup
import requests
import re
from urllib.request import urlopen
import time
from lxml import etree

requests_url = 'https://boymeetsshe.stores.jp/login'   

session = requests.Session()         #Session

page = session.get(requests_url).text    
soup = BeautifulSoup(page, "lxml")    
soup_token = soup.find_all(attrs={"name": 'csrf-token'})         #This is for the token
token = soup_token[0]['content']    


login = {
            "email": "MYEMAIL",
            "password": "MYPASSWORD"
        }

header = {
            'Referer': 'https://boymeetsshe.stores.jp/login?redirect_uri=/',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
            'authority': 'boymeetsshe.stores.jp',
            'method': 'POST',
            'x-csrf-token':token
         }

session.post(requests_url, data = login, headers = header)
page1 = session.get('https://boymeetsshe.stores.jp/mypage/settings',headers = header)
print(page1.text)

I am not sure if I made a stupid mistake or not, but I really tried a lot of solutions already and still can't figure out my problems. Thanks for any help.

I think it may be a good idea to use selenium for login, read the documentation and use it, but I try to say here how to use it.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

import os
import time

chromedriver = "driver/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver

driver = webdriver.Chrome(chromedriver)
actions = ActionChains(driver)

username_login = 'MYEMAIL'
password_login = 'MYPASSWORD'


def login(driver , username_login ,password_login):

    login_link = 'https://boymeetsshe.stores.jp/login'
    driver.get(login_link)
    time.sleep(5)

    username_input = driver.find_element_by_xpath('that input element XPath or css selector')
    password_input = driver.find_element_by_xpath('that input element xpath or css selector')

    username_input.send_keys(username_login)
    password_input.send_keys(password_login)

    login_button = driver.find_element_by_xpath('that login button element xpath or css selector')
    login_button.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM