【selenium】selenium和requests登陆的区别

requests登陆

import requests
import time
t = int(time.time()*1000)
# 创建一个会话
s = requests.Session()
post_url = 'http://account.chinaunix.net/login/login'
data = {
    'username': 'xxxxxxxxx',
    'password': 'xxxxxxxxx',
    '_token': 'nmSXhgHib8dTIC9DrATSkTzBBo4zz9eqDEPeG5i1',
    '_t': t
}
r = s.post(url=post_url, data=data,allow_redirects=False)
r = s.get('http://bbs.chinaunix.net/')
with open('chinaunix.html', 'w') as fp:
    fp.write(r.text)

selenium登陆

from selenium import webdriver
import  time
url = 'http://bbs.chinaunix.net/member.php?mod=logging&action=login&logsubmit=yes'

webdriver = webdriver.Chrome()


webdriver.get(url=url)

time.sleep(15)


webdriver.find_element_by_name('username').send_keys('xxxxxxxxx')

webdriver.find_element_by_name('password').send_keys('xxxxxxxxx')

webdriver.find_element_by_name('loginsubmit').click()

time.sleep(15)

with open('chinaunixselenium.html','w',encoding='utf-8') as fp:
    fp.write(webdriver.page_source)


使用Selenium
需要安装:pip install selenium
下载:http://chromedriver.storage.googleapis.com/index.html

下面是谷歌浏览器与chromedriver的版本对应关系,供参考:

------------2019年兼容版本对照表-----------
 

 注:支持chromeV74版本的driver版本号比较特别一点,不知道之后会不会回归以前正常的版本

ChromeDriver 78.0.3904.11 (2019-09-12)---------Supports Chrome version 78
ChromeDriver 77.0.3865.40 (2019-08-20)---------Supports Chrome version 77
ChromeDriver 76.0.3809.12 (2019-06-07)---------Supports Chrome version 76
ChromeDriver 75.0.3770.8 (2019-04-29)---------Supports Chrome version 75
ChromeDriver v74.0.3729.6 (2019-03-14)--------Supports Chrome v74
ChromeDriver v2.46 (2019-02-01)----------Supports Chrome v71-73
--------以下为2018年兼容版本对照表-------

ChromeDriver v2.45 (2018-12-10)----------Supports Chrome v70-72
ChromeDriver v2.44 (2018-11-19)----------Supports Chrome v69-71
ChromeDriver v2.43 (2018-10-16)----------Supports Chrome v69-71
ChromeDriver v2.42 (2018-09-13)----------Supports Chrome v68-70
ChromeDriver v2.41 (2018-07-27)----------Supports Chrome v67-69
ChromeDriver v2.40 (2018-06-07)----------Supports Chrome v66-68
ChromeDriver v2.39 (2018-05-30)----------Supports Chrome v66-68
ChromeDriver v2.38 (2018-04-17)----------Supports Chrome v65-67
ChromeDriver v2.37 (2018-03-16)----------Supports Chrome v64-66
ChromeDriver v2.36 (2018-03-02)----------Supports Chrome v63-65
ChromeDriver v2.35 (2018-01-10)----------Supports Chrome v62-64
 
 


 

你可能感兴趣的:(Selenium,Python)