本文目錄一覽:
如何使用python進行瀏覽器行為模擬
你可以使用python的webbrowser庫來模擬瀏覽器:
url = ” # Open URL in a new tab, if a browser window is already open.webbrowser.open_new_tab(url + ‘doc/’) # Open URL in new window, raising the window if possible.webbrowser.open_new(url)或者使用python的第三方庫, selenium
from selenium import webdriverfrom selenium.webdriver.common.keys import Keys browser = webdriver.Firefox() browser.get(‘)assert ‘Yahoo!’ in browser.title elem = browser.find_element_by_name(‘p’) # Find the search boxelem.send_keys(‘seleniumhq’ + Keys.RETURN) browser.quit()
怎樣利用python打開一個網頁並實現自動登錄
登陸其實就是將賬號密碼之類的POST到那個網站的伺服器。你可以通過抓包看到你點擊登陸時發的POST包。那麼你用python也模擬發一個一樣的包給伺服器,就實現了模擬登陸唄。
Python Request庫模擬登錄
1.讀取本地用戶文件,或者賬號名和密碼
2.使用md5對用戶密碼進行加密
3.獲取session的臨時隨機串及sessionid
4.對隨機串及用戶密碼進行二次md5加密生成新的請求秘鑰串
5.傳遞用戶名、新的秘鑰串及sessionid模擬用戶登錄
python3模擬登錄有哪些情況
使用谷歌瀏覽器F12查看登錄請求內容
1.request header需要參數:User-Agent、Referer等。
2.post內容。
python 3.x中urllib庫和urilib2庫合併成了urllib庫。
urllib2.urlopen()變成了urllib.request.urlopen()
urllib2.Request()變成了urllib.request.Request()
cookielib 模塊-》http.cookiejar
#! /usr/bin/env python
# -*- coding:gb2312 -*-
# __author__=”zhaowei”
”’
python3.4
模擬登錄鄭州公積金網站,查詢繳存至月份。
”’
from html.parser import HTMLParser
import urllib
import http.cookiejar
import string
import re
hosturl = ”
posturl = ”
cj = http.cookiejar.CookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
h = urllib.request.urlopen(hosturl)
headers = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0’,
‘Referer’: ”}
postData = {‘selectlb’: ‘1’,#登錄模式,身份證2,賬號1
‘username’: ‘1423141234’, #公積金賬號
‘radename’: ‘趙威’,#姓名
‘mm’: ‘88888’,#密碼
‘submit322’: ‘確認’#固定值
}
postData = urllib.parse.urlencode(postData, encoding=’gb2312′).encode(‘gb2312’)
#因為post裡面有中文,因此需要先把url經過gb2312編碼處理,然後再把請求編碼為gb2312位元組碼(post必須是位元組碼)。
request = urllib.request.Request(posturl, postData, headers)
response = urllib.request.urlopen(request)
text = response.read()
html = text.decode(‘gb2312’)
hgjj_last_data = re.findall(‘tdp繳至月份:/p(\s*)/td(\s*)td(.*?)/td’, html)
#使用正則表達式匹配繳至月份
print(hgjj_last_data[0][2])
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/256978.html