我使用POST请求在python中编写了一些代码来从网页中获取特定数据。但是,当我运行它时,除了空白控制台外,我得不到任何结果。我已经尝试相应地填写请求参数。也许,我无法注意哪些应该包含在参数中。我正在处理的页面包含右侧面板中的几个图像。当点击图像时,我在这里谈论的请求被发送到服务器并带回结果并显示有关其“味道”的新信息。我的目标是解析与每个图像相关的所有风格。无论如何,我试图附上所有必要的东西,以找出我所缺少的东西。提前谢谢。
这是我从chrome开发人员工具中获得的,用于准备POST请求:
===================================================================================
General:
Request URL:https://www.optigura.com/product/ajax/details.php
Request Method:POST
Status Code:200 OK
Response Headers:
Cache-Control:no-store, no-cache, must-revalidate
Cache-Control:max-age=0, no-cache, no-store, must-revalidate
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:782
Content-Type:text/html; charset=utf-8
Request Headers:
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:34
Content-Type:application/x-www-form-urlencoded
Cookie:OGSESSID=s1qqd0euokbfrdub9pf2efubh1; _ga=GA1.2.449310094.1501502802; _gid=GA1.2.791686763.1501502802; _gat=1; __atuvc=1%7C31; __atuvs=597f1d5241db0352000; beyable-TrackingId=499b4c5b-2939-479b-aaf0-e5cd79f078cc; aaaaaaaaa066e9a68e5654b829144016246e1a736=d5758131-71db-41e1-846d-6d719d381060.1501502805122.1501502805122.$bey$https%3a%2f%2fwww.optigura.com%2fuk%2fproduct%2fgold-standard-100-whey%2f$bey$1; aaaaaaaaa066e9a68e5654b829144016246e1a736_cs=; aaaaaaaaa066e9a68e5654b829144016246e1a736_v=1.1.0; checkloc-uk=n
Host:www.optigura.com
Origin:https://www.optigura.com
Referer:https://www.optigura.com/uk/product/gold-standard-100-whey/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data:
opt:flavor
opt1:207
opt2:47
ip:105
=======================================================================================
以下是我正在尝试的内容:
import requests
from lxml import html
payload = {"opt":"flavor","opt1":"207","opt2":"47","ip":"105"}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'}
response = requests.post("https://www.optigura.com/product/ajax/details.php", params = payload, headers = headers).text
print(response)
这是该网页的原始链接:
https://www.optigura.com/uk/product/gold-standard-100-whey/
答案 0 :(得分:4)
您没有在POST正文中发送值,params
设置了URL查询参数。请改用data
:
response = requests.post(
"https://www.optigura.com/product/ajax/details.php",
data=payload,
headers=headers)
您可能需要设置引荐来源标头(将'Referer': 'https://www.optigura.com/uk/product/gold-standard-100-whey/'
添加到标头词典中),并使用session object来捕获和管理Cookie(首先向https://www.optigura.com/uk/product/gold-standard-100-whey/
发出GET请求)
通过一些实验,我注意到该网站还要求在{#1}}标题回复之前设置它。
以下作品:
X-Requested-With
响应来自JSON数据:
with requests.session():
session.get('https://www.optigura.com/uk/product/gold-standard-100-whey/')
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
'Referer': 'https://www.optigura.com/uk/product/gold-standard-100-whey/',
'X-Requested-With': 'XMLHttpRequest'
}
response = session.post(
"https://www.optigura.com/product/ajax/details.php",
data=payload, headers=headers)
答案 1 :(得分:3)
您应该尝试以下请求结构:
要发送的数据:
data = {'opt': 'flavor', 'opt1': '207', 'opt2': '47', 'ip': 105}
接头:
headers = {'X-Requested-With': 'XMLHttpRequest'}
URL:
url = 'https://www.optigura.com/product/ajax/details.php'
您还需要获取Cookie,因此需要requests.session()
:
s = requests.session()
r = s.get('https://www.optigura.com/uk/product/gold-standard-100-whey/')
cookies = r.cookies
完整请求:
response = s.post(url, cookies=cookies, headers=headers, data=data)
现在,您可以获得所需的HTML
作为
print(response.json()['info2'])
输出:
'<ul class="opt2"><li class="active">
<label>
<input type="radio" name="ipr" value="1360" data-opt-sel="47" checked="checked" /> Delicious Strawberry - <span class="green">In Stock</span></label>
</li><li>
<label>
<input type="radio" name="ipr" value="1356" data-opt-sel="15" /> Double Rich Chocolate - <span class="green">In Stock</span></label>
</li><li>
<label>
<input type="radio" name="ipr" value="1169" data-opt-sel="16" /> Vanilla Ice Cream - <span class="green">In Stock</span></label>
</li></ul>'
然后您可以使用lxml
来剔除风味值:
from lxml import html
flavors = response.json()['info2']
source = html.fromstring(flavors)
[print(element.replace(' - ', '').strip()) for element in source.xpath('//label/text()[2]')]
输出:
Delicious Strawberry
Double Rich Chocolate
Vanilla Ice Cream