Python crawler learning (3)

Use the third-party library requests and BeautifulSoup to implement the login function:

import requests
from bs4 import BeautifulSoup
 
headinfo = {
    'Connection': 'Keep-Alive',
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
url = 'http://sspanel.badtom.cn/auth/login'
session = requests.session()
login_data = {'email': '[email protected]', 'passwd': '123456'}
session.post(url, login_data, headers = headinfo)
page = session.get('http://sspanel.badtom.cn/user')
data = BeautifulSoup(page.text, "html.parser")
 
print(data.title.text)
for x in data.findAll('a'):
    print(x['href'])
Among them, session.post(url, login_data, headers = headinfo) is to send a post request to the server for login, and the session will be maintained after the login is successful. If you comment out this sentence, you will find that the returned web page is the data before logging in.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685588&siteId=291194637