Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 给出空结果的POST请求_Python_Python 3.x_Post_Web Scraping - Fatal编程技术网

Python 给出空结果的POST请求

Python 给出空结果的POST请求,python,python-3.x,post,web-scraping,Python,Python 3.x,Post,Web Scraping,我已经用python编写了一些代码,使用POST请求从网页获取特定数据。然而,当我运行它时,除了一个空白的控制台之外,我没有得到任何结果。我已尝试相应地填写请求参数。也许,我注意不到参数中应该包括哪些。我正在处理的页面在其右侧面板中包含多个图像。当点击一个图像时,我在这里谈论的请求被发送到服务器,并返回结果,并在其下显示有关其“味道”的新信息。我的目标是解析连接到每个图像的所有味道。不管怎样,我正在尝试附加所有必要的东西来找出我缺少的东西。提前谢谢 这是我从chrome developer to

我已经用python编写了一些代码,使用POST请求从网页获取特定数据。然而,当我运行它时,除了一个空白的控制台之外,我没有得到任何结果。我已尝试相应地填写请求参数。也许,我注意不到参数中应该包括哪些。我正在处理的页面在其右侧面板中包含多个图像。当点击一个图像时,我在这里谈论的请求被发送到服务器,并返回结果,并在其下显示有关其“味道”的新信息。我的目标是解析连接到每个图像的所有味道。不管怎样,我正在尝试附加所有必要的东西来找出我缺少的东西。提前谢谢

这是我从chrome developer tools获得的准备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/

您没有在帖子正文中发送值,
参数设置URL查询参数。使用
数据

response = requests.post(
    "https://www.optigura.com/product/ajax/details.php",
    data=payload, 
    headers=headers)
您可能需要设置一个Referer标题(添加
'Referer':'https://www.optigura.com/uk/product/gold-standard-100-whey/“
到您的标题字典),并使用捕获和管理cookie(向
https://www.optigura.com/uk/product/gold-standard-100-whey/
首先)

通过一些实验,我注意到该站点还要求设置
X-request-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()

您没有在帖子正文中发送值,
params
设置URL查询参数。使用
数据

response = requests.post(
    "https://www.optigura.com/product/ajax/details.php",
    data=payload, 
    headers=headers)
您可能需要设置一个Referer标题(添加
'Referer':'https://www.optigura.com/uk/product/gold-standard-100-whey/“
到您的标题字典),并使用捕获和管理cookie(向
https://www.optigura.com/uk/product/gold-standard-100-whey/
首先)

通过一些实验,我注意到该站点还要求设置
X-request-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()

您应该尝试以下请求结构:

  • 要发送的数据:

    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
as

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>'
Delicious Strawberry
Double Rich Chocolate
Vanilla Ice Cream
输出:

'<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>'
Delicious Strawberry
Double Rich Chocolate
Vanilla Ice Cream

您应该尝试以下请求结构:

  • 要发送的数据:

    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
as

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>'
Delicious Strawberry
Double Rich Chocolate
Vanilla Ice Cream
输出:

'<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>'
Delicious Strawberry
Double Rich Chocolate
Vanilla Ice Cream

您没有在帖子正文中发送值,
params
设置URL查询参数。改为使用
data
。您不是在帖子正文中发送值,
params
设置URL查询参数。使用
数据
来代替。试着按照你建议的Martijn Pieters爵士去做。不过对我来说有点高级了!试着应付你建议的Martijn Pieters爵士。不过对我来说有点高级了!哦,我的上帝!!多么精辟的回答!!它做了我想做的事。马尔蒂恩·皮特斯爵士也提出了同样的建议,但我无法正确地理解事情应该如何发展。非常感谢先生,安德森。你让我开心。哦,我的上帝!!多么精辟的回答!!它做了我想做的事。马尔蒂恩·皮特斯爵士也提出了同样的建议,但我无法正确地理解事情应该如何发展。非常感谢先生,安德森。你让我开心。