Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use python to log in to 24 mainstream websites

Shulou Source: shulou.com Published: 2022-06-02 02:45:05 10月04日 Update

How to use python to log on to 24 mainstream websites, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Crawler scripts are often used, so you can't avoid logging in.

Using Python generally requires the request library to supplement the post elements in header, and some have hidden hidden parameters, which can be found through browser F12 or element review, which is a pit for beginners.

There is also a need to solve the problem of verification code. One way is to download the verification code image identification verification code and post again, or use the cloud coding platform. Of course, some CAPTCHAs and their perverts are not so easy to solve, such as word selection order, sliders, and 12306.

Completed mainstream websites

Above are some of the mainstream websites that the author has completed, some of which log in through selenium, some simulate login directly by grabbing the package, and some use the scrapy framework.

This is easy to understand, because the design of some websites is more complex, and it is difficult to achieve simulated login by grabbing the package, so it is relatively easy to use selenium+webdriver.

Although selenium is used when logging in, for the sake of efficiency, we can maintain the cookie after logging in, and then call requests or scrapy for data collection, so that the speed of data collection can be guaranteed.

Simulated login to GitHub

Here to show you a simulation login GitHub code.

"

The second login method of github

Info:

Author:CriseLYJ

Github: https://github.com/CriseLYJ/

Update_time:2019-3-7

"

Import re

Import requests

From lxml import etree

Class Login (object):

Class GithubLogin (object):

Def _ _ init__ (self, email, password):

# initialization Information

Self.headers = {

'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 1014.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'

'Referer': 'https://github.com/',

'Host': 'github.com'

}

Self.session = requests.Session ()

Self.login_url = 'https://github.com/login'

Self.post_url = 'https://github.com/session'

Self.session = requests.Session ()

Self.email = email

Self.password = password

# simulated login

Def login_GitHub (self):

# login entry

Post_data = {

'commit': 'Sign in'

'utf8': '✓'

'authenticity_token': self.get_token ()

'login': self.email

'password': self.password

}

Resp = self.session.post (

Self.post_url, data=post_data, headers=self.headers)

Print ('StatusCode:', resp.status_code)

If resp.status_code! = 200:

Print ('Login Fail')

Match = re.search (r'"user-login" content= "(. *?)", resp.text)

User_name = match.group (1)

Print ('UserName:', user_name)

Response = self.session.post (self.post_url, data=post_data, headers=self.headers)

Print (response.status_code)

Print (post_data)

If response.status_code = = 200:

Print ("login succeeded!")

Else:

Print ("login failed!")

# get token information

# Get login token

Def get_token (self):

Response = self.session.get (self.login_url, headers=self.headers)

Html = etree.HTML (response.content.decode ())

Token = html.xpath ('/ / input [@ name= "authenticity_token"] / @ value') [0]

Return token

If response.status_code! = 200:

Print ('Get token fail')

Return None

Match = re.search (

Response.text name = "authenticity_token" value= "(. *?)", response.text)

If not match:

Print ('Get Token Fail')

Return None

Return match.group (1)

If _ _ name__ = ='_ _ main__':

Email = input ('Please enter your account number:')

Password = input ('Please enter your password:')

Email = input ('Account:')

Password = input ('Password:')

Login = Login (email, password)

Login = GithubLogin (email, password)

Login.login_GitHub ()

I believe this is a good tutorial for beginners of reptiles.

But as a hint, the simulated login code may fail at any time, because the structure of the front-end web pages such as HTML, CSS, JS and so on may change according to the company's business adjustment.

So, the key point is to master a variety of skills, learn these can debug their own login, then you can also become a contributor!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Login website authentication mainstream there is plenty of code information data data collection time crawler help input complexity clarity business elements entry again beginners. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno Xiaomi Shulou Technology Huawei