Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

python - 如何在python中始终保持cookies持久用于requests.session(how to keep cookies persistent all the time for requests.session in python)

i am using python requests to get some information from company site, first, i need to login, then use beautifulsoup to get some other url, and go to these url to get information,

(我正在使用python请求从公司网站获取一些信息,首先,我需要登录,然后使用beautifulsoup获取其他网址,然后转到这些网址获取信息,)

i use session, but now the problem is: after i login,get the url, then go to these url, and find the text return from these url is not what i want, not sure whether it is because of non-persistent cookies,

(我使用会话,但是现在的问题是:登录后,获取url,然后转到这些url,然后找到从这些url返回的文本不是我想要的,不确定是否是由于非持久性cookie,)

s = requests.session()
s.post(url = "https://login.company.com/login/login.do",data={'uid':user,'password':
password,'actionFlag': 'loginAuthenticate'})
r=s.get("http://3ms.company.com/hi/space/?l=zh-cn")
soup = BeautifulSoup(r.text,'html.parser')
div=soup.find('div',attrs={'class':'top_pop mt10'})
for a in div.find_all('li'):
    url=a.find('div',attrs={'class':'top_pop_P_right fn'}).find('a')['href']
    r1=s.get(url)
    print(r1.text)

i try to use the following codes to update cookies, but do not work,

(我尝试使用以下代码更新Cookie,但不起作用,)

if r1.cookies.get_dict():
  s.cookies.update(r1.cookies)

any idea on how to resolve this problem ?

(关于如何解决此问题的任何想法?)

  ask by tonyibm translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...