request 无法获取cookie

mengweilil 2017-01-14 12:43:25
或者session或者任何东西。
通过firefox访问 http://hd.chinatax.gov.cn/guoshui/main.jsp
发现response里面有jsessionid,也就是有cookie。

但是使用requests,就是获取不到。

下面是源代码:说明:headers的内容,和浏览器(火狐)的devtools看到的完全一样。

import requests

url = 'http://hd.chinatax.gov.cn/guoshui/main.jsp'

headers = {
"Host":"hd.chinatax.gov.cn",
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language":"zh-CN,en-US;q=0.7,en;q=0.3",
"Accept-Encoding":"gzip, deflate",
"Connection":"keep-alive",
"Upgrade-Insecure-Requests":"1",
"Cache-Control":"max-age=0",
}
conn = requests.session()
resp = conn.put(url,headers=headers)
# 打印请求的头
print(resp.request.headers)
print(resp.headers)
print(resp.history)
print(resp.status_code)


print(conn)

for x in conn.cookies:
print(x)
if __name__ == '__main__':
pass
...全文
572 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengweilil 2017-01-16
  • 打赏
  • 举报
回复
引用 2 楼 u012536120 的回复:
[quote=引用 1 楼 mengweilil 的回复:]
requests,put,get,post方法都用过,无效。

我去试了试,Python2的cookie能打印出来。Python3的不可以。
然后我用Python3的代码打印百度的,能出cookie。
问题没解决。

下面是我的代码,贴出了做参考


#for python3
import urllib
from http.cookiejar import CookieJar

url='http://hd.chinatax.gov.cn/guoshui/main.jsp'
#url="https://www.baidu.com"
cookie = CookieJar()
handler=urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
request=urllib.request.install_opener(opener)

response = opener.open(url).read()
for item in cookie:
print('Name = %s' % item.name)
print('Value = %s' % item.value)
print('-----------------')

"""
#for python2
import urllib2
import cookielib

url='http://hd.chinatax.gov.cn/guoshui/main.jsp'
cookie = cookielib.CookieJar()
handler=urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)

response = opener.open(url)
for item in cookie:
print 'Name:',item.name
print 'Value:',item.value
print '-----------------'
"""
[/quote]

嗯,我也发现了同样的问题,python2能够获取完整的头部,python3不行。我用requests试了,还给requests的作者发邮件了。
看来网上用爬虫的,都是用python2的啊。根本找不到答案。
sanGuo_uu 2017-01-15
  • 打赏
  • 举报
回复
引用 1 楼 mengweilil 的回复:
requests,put,get,post方法都用过,无效。
我去试了试,Python2的cookie能打印出来。Python3的不可以。 然后我用Python3的代码打印百度的,能出cookie。 问题没解决。 下面是我的代码,贴出了做参考


#for python3
import urllib
from http.cookiejar import CookieJar

url='http://hd.chinatax.gov.cn/guoshui/main.jsp'
#url="https://www.baidu.com"
cookie = CookieJar()
handler=urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
request=urllib.request.install_opener(opener)
 
response = opener.open(url).read()
for item in cookie:
    print('Name = %s' % item.name)
    print('Value = %s' % item.value)
    print('-----------------')

"""
#for python2
import urllib2
import cookielib

url='http://hd.chinatax.gov.cn/guoshui/main.jsp'
cookie = cookielib.CookieJar()
handler=urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)

response = opener.open(url)
for item in cookie:
    print 'Name:',item.name
    print 'Value:',item.value
    print '-----------------'
"""
mengweilil 2017-01-14
  • 打赏
  • 举报
回复
requests,put,get,post方法都用过,无效。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧