BackgroundInfo:
我正在刮亚马逊。在使用request.session.get()获得URL的页面源代码的最终版本之前,我需要设置会话cookie。
码:
import requests
# I am currently working in China, so it's cn.
# Use the homepage to get cookies. Then use it later to scrape data.
homepage = 'http://www.amazon.cn'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
response = requests.get(homepage,headers = headers)
cookies = response.cookies
#set up the Session object, so as to preserve the cookies between requests.
session = requests.Session()
session.headers = headers
session.cookies = cookies
#now begin download the source code
url = 'https://www.amazon.cn/TCL-%E7%8E%8B%E7%89%8C-L65C2-CUDG-65%E8%8B%B1%E5%AF%B8-%E6%96%B0%E7%9A%84HDR%E6%8A%80%E6%9C%AF-%E5%85%A8%E6%96%B0%E7%9A%84%E9%87%8F%E5%AD%90%E7%82%B9%E6%8A%80%E6%9C%AF-%E9%BB%91%E8%89%B2/dp/B01FXB0ZG4/ref=sr_1_2?ie=UTF8&qid=1476165637&sr=8-2&keywords=L65C2-CUDG'
response = session.get(url)
预期结果:
当导航到Chrome中的亚马逊首页时,Cookie应该类似于:
如您所见,在Cookie部分(我用红色强调)中,响应我们对首页请求的响应所设置的部分Cookie是“ ubid-acbcn”,这也是请求标头的一部分,可能是上次访问时留下的。
这就是我想要的cookie,我试图通过上面的代码来获取。
在python代码中,它应该是cookieJar或字典。无论哪种方式,其内容都应该包含“ ubid-acbcn”和“ session-id”:
{'ubid-acbcn':'453-7613662-1073007','session-id':'455-1363863-7141553','otherparts':'otherparts'}
我得到的是:
“会话ID”在那里,但是“ ubid-acbcn”丢失了。
>>homepage = 'http://www.amazon.cn'
>>headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
>>response = requests.get(homepage,headers = headers)
>>cookies = response.cookies
>>print(cookies.get_dict()):
>>{'session-id': '456-2975694-3270026','otherparts':'otherparts'}
相关资料:
作业系统:WINDOWS 10
PYTHON:3.5
要求:2.11.1
很抱歉,我有点冗长。
我尝试过的结果:
我用Google搜索了某些关键字,但似乎没人面对这个关键字
问题。
我认为这可能与亚马逊有关
防刮擦措施。但是除了改变我的标题来伪装
作为一个人,我知道我应该做的事情不多。
我也接受了tt可能不是缺少cookie的情况的可能性。但是我没有正确设置我的request.get(homepage,headers = headers),因此response.cookie与预期的不一样。鉴于此,我试图在浏览器中复制请求标头,仅保留cookie部分,但是响应cookie仍然缺少“ ubid-acbcn”部分。也许必须设置其他参数?
参考方案
您正在尝试从简单的“无名” GET
请求中获取Cookie。但是,如果将其“代表”发送给Session
,则可以获得所需的ubid-acbcn
值:
session = requests.Session()
homepage = 'http://www.amazon.cn'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}
response = session.get(homepage,headers = headers)
cookies = response.cookies
print(cookies.get_dict())
输出:
{'ubid-acbcn': '456-2652288-5841140' ...}
为什么dict.get(key)起作用但dict [key]不起作用? - python我正在尝试根据字符串中存在的1将某些数字的二进制字符串分组在一起。这不起作用:s = "0 1 3 7 8 9 11 15" numbers = map(int, s.split()) binaries = [bin(x)[2:].rjust(4, '0') for x in numbers] one_groups =…
不带括号的调用函数的目的python - python考虑以下:class objectTest(): def __init__(self,a): self.value = a def get_value(self): return self.value class execute(): def __init__(self): a = objectTest(1) b = objectTest(1) print(…
如何用'-'解析字符串到节点js本地脚本? - python我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…
将字符串分配给numpy.zeros数组[重复] - pythonThis question already has answers here: Weird behaviour initializing a numpy array of string data (4个答案) …
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。