【问题标题】:POST request giving empty resultsPOST 请求给出空结果
【发布时间】:2017-07-31 19:20:17
【问题描述】:

我在 python 中编写了一些代码,使用 POST 请求从网页中获取特定数据。但是,当我运行它时,除了空白控制台之外,我什么也得不到。我已经尝试相应地填写请求参数。也许,我无法注意到参数中应该包含哪些内容。我正在处理的页面在其右侧面板中包含多个图像。当单击图像时,我在这里谈论的请求被发送到服务器并带回结果并在其下显示有关其风味的新信息。我的目标是解析与每个图像相关的所有风味。无论如何,我正在尝试附上所有必要的东西,以找出我缺少的东西。提前致谢。

这是我从 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/

【问题讨论】:

  • 您没有在 POST 正文中发送值,params 设置了 URL 查询参数。请改用data

标签: python python-3.x post web-scraping


【解决方案1】:

你应该试试下面的请求结构:

  • 要发送的数据:

    data = {'opt': 'flavor', 'opt1': '207', 'opt2': '47', 'ip': 105}
    
  • 标题:

    headers = {'X-Requested-With': 'XMLHttpRequest'}
    
  • 网址:

    url = 'https://www.optigura.com/product/ajax/details.php'
    
  • 你还需要获取cookies,所以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

【讨论】:

  • 天哪!多么详尽的答案!它完全符合我的要求。 Martijn Pieters 爵士也提出了同样的建议,但我无法正确地弄清楚事情应该如何。非常感谢安德森先生。你让我开心。
【解决方案2】:

您没有在 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 请求)。

通过一些实验,我注意到该网站还要求设置 X-Requested-With 标头,然后才能响应内容。

以下作品:

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)

响应以 JSON 数据的形式出现:

data = response.json()

【讨论】:

  • 试图解决您对 Martijn Pieters 先生的建议。不过对我来说有点高级!
猜你喜欢
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2017-01-22
  • 2021-06-08
  • 2019-05-16
  • 1970-01-01
  • 2019-12-12
  • 2013-09-06
相关资源
最近更新 更多