有什么办法可以通过python登录网站

我要登录,然后爬网该网站。

我通过python登录后,必须获得每个班级(员工级别)的薪水。

登录网址= https://www.jobplanet.co.kr/users/sign_in 每个班级的工资网址= https://www.jobplanet.co.kr/companies/20575/salaries/

from bs4 import BeautifulSoup
import urllib,http.cookiejar
cj = http.cookiejar.LWPCookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) 
urllib.request.install_opener(opener)

headers = {'User-Agent': 'Mozilla/5.0'}

params = urllib.parse.urlencode({"mode":"login","user_email":"*******","user_password":"******"})
params = params.encode('utf-8')
req = urllib.request.Request("https://www.jobplanet.co.kr/users/sign_in",headers=headers)
rej = urllib.request.Request("https://www.jobplanet.co.kr/companies/20575/salaries/",headers=headers)
res = opener.open(rej)

html = res.read()
luvlu 回答:有什么办法可以通过python登录网站

您必须找出要从中提取源代码的urlLoginaurlAuthurlBd(网址正文)。 您可以使用Chrome或Firefox devtools(在Windows上为F12键)。

您有urlLogin,但aurlAuth可能有所不同,使用浏览器的devtool并进行实际登录,当您单击“登录”按钮时,您会在“网络”标签中看到请求的详细信息。 (不要忘记点击网络复选框)

import requests

urlLogin = 'https://example.com/jsp/login.jsp'
urlAuth = 'https://example.com/CheckLoginServlet'
urlBd = 'https://example.com/jsp/batchdownload.jsp'
payload = {
    "username": "username","password": "password"
}

# Session will be closed at the end of with block
with requests.Session() as s:
    s.get(urlLogin)
    headers = s.cookies.get_dict()
    print(f"Session cookies {headers}")
    # Use headers if you want to append your own headers to default
    r1 = s.post(urlAuth,data=payload,headers=headers)
    # Here header is optional,code could be 
    # r1 = s.post(urlAuth,data=payload) 
    print(f'MainFrame text:::: {r1.status_code}')  #200

    r2 = s.post(urlBd,data=payload)
    print(f'MainFrame text:::: {r2.status_code}')  #200
    print(f'MainFrame text:::: {r2.text}')  #page source

    # 3. Again cookies will be used through session to access batch download page
    r2 = s.post(config['access-url'])
    print(f'Batch Download status:::: {r2.status_code}')  #200
    source_code = r2.text
    # print(f'Batch Download source:::: {source_code}')
本文链接:https://www.f2er.com/3115956.html

大家都在问