第三方库:browsercookie
可以很轻易的获取浏览器cookie,访问需要需要登录才能查看的页面
pipy主页: https://pypi.org/project/browsercookie/
代码示例
# 获取浏览器cookie import browsercookie import requests from bs4 import BeautifulSoup # 消除 warning InsecureRequestWarning from requests.packages import urllib3 urllib3.disable_warnings() # 获取cookie,前提是需要浏览器登陆过 chrome_cookie = browsercookie.chrome() # for cookie in chrome_cookie: # print(cookie) headers = {"User-Agent": "Mozilla/5.0"} # 获取页面信息 response = requests.get("https://my.csdn.net/", cookies=chrome_cookie, headers=headers, verify=False) # 解析获取的页面 soup = BeautifulSoup(response.text, features="lxml") person_sign = soup.find(name="dd", attrs={"class", "person-sign"}) # 出去结果 print(person_sign.text) # 要成为程序员的男人,个人主页:www.pengshiyu.com