python requests 带cookie访问页面
2016-08-09 22:06
211 查看
背景:
获取需要登录的页面,可在登陆后将获取到的cookie添加到请求的脚本中。
方法:
import requests
url="http://webserver/admin"
cookies = dict(cookies_are='cookie内容')
##cookie内容可在登陆页面后,通过浏览器工具()获取。
response = requests.get(url, cookies=cookies)
print response.content.decode("utf-8")
这样就可以获取登陆后的网页源代码。
如果要模拟的更真实点,加上user agent会更好些。
my_headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2526.80 Safari/537.36 Core/1.45.933.400 QQBrowser/9.0.8699.400', 'Accept-Encoding' : 'gzip, deflate, sdch'}
response = requests.get(url, cookies=cookies,headers=my_headers)
print response.content.decode("utf-8")
只要不被专门盯上,一般应该会被服务器当做正常访问来对待的。
获取需要登录的页面,可在登陆后将获取到的cookie添加到请求的脚本中。
方法:
import requests
url="http://webserver/admin"
cookies = dict(cookies_are='cookie内容')
##cookie内容可在登陆页面后,通过浏览器工具()获取。
response = requests.get(url, cookies=cookies)
print response.content.decode("utf-8")
这样就可以获取登陆后的网页源代码。
如果要模拟的更真实点,加上user agent会更好些。
my_headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2526.80 Safari/537.36 Core/1.45.933.400 QQBrowser/9.0.8699.400', 'Accept-Encoding' : 'gzip, deflate, sdch'}
response = requests.get(url, cookies=cookies,headers=my_headers)
print response.content.decode("utf-8")
只要不被专门盯上,一般应该会被服务器当做正常访问来对待的。
相关文章推荐
- python3.4 获取cookie后继续访问其他页面
- python2-爬虫-带上cookie访问登陆后可见的页面
- iframe跨域访问cookie问题, A站iframe引用其它站(B站)的内容时,B站的页面获取不到B站种下的Cookies。
- js_Cookie 设置访问页面
- 利用python2.7自带的简单的web服务器SimpleHTTPServer实现web页面的访问
- Python Cookie操作实战---requests&selenium&urllib2
- cookie不同页面访问不到的问题
- 编写Python程序访问需要cookie的网页
- requests抓取需登录(cookie)的页面
- python利用cookie登录网站进行访问
- 通过cookie记录,设置页面访问的跳转页
- python requests https 访问出错
- 今天使用ie6访问有iframe页面时,出现cookie被阻挡的问题解决方法
- Python get获取页面cookie
- (转)用python获取页面返回的cookie
- JS Cookie怎样设置当天有效?在Cookie保存用户访问过的页面?
- (ASP.NET)通过Cookie对象实现页面访问计数功能。
- Python 使用Cookie访问
- python requests 自动管理 cookie 。 get后进行post发送数据---》最简单的刷票
- python 携带cookie访问网站(python接口测试post)