我正在使用python來解析一些網頁。我用漂亮的腳本來分析一個登錄頁面和發送有效載荷登錄:Python:維護會話以便登錄後訪問所有頁面
import bs4 as bs
import urllib.request
import requests
import webbrowser
import urllib.parse
primary_url = "SOME URL YOU WANT TO LOG IN"
sauce = urllib.request.urlopen(primary_url).read()
soup = bs.BeautifulSoup(sauce,"html.parser")
for form_list in soup.find_all('form'):
action_value = form_list.get('action')
action_url = urllib.parse.urljoin(primary_url, action_value)
method_value = form_list.get('method')
if (method_value == "post"):
payload = dict()
inputs1 = form_list.findAll('input',type ="email")
for i in inputs1:
input2 = i.get('name')
script_value = 'USER-EMAIL'
payload[input2] = script_value
inputs2 = form_list.findAll('input',type ="password")
for j in inputs2:
input3 = j.get('name')
script_value = 'PASSWORD-FOR-EMAIL'
payload[input3] = script_value
r = requests.post(action_url, payload)
with open("result_page.html", "w") as f:
f.write(str(r.content))
sessionid = r.cookies.get('SESSIONID') #ERROR
print(sessionid) #ERROR
# CODE TO ACCESS FURTHER PAGES . . .
但是我不能夠再訪問頁面,因爲我需要登錄才能訪問這些頁面。
爲了能夠在您的進一步要求使用cookie,但仍然可以登錄您應該放眼['requests.session()'](http://docs.python-requests.org/en/master/user/高級/#會話對象) – Andersson
我如何維護會話,直到我解析網頁的所有頁面? –
@MaccenWright創建一個會話對象。 –