【问题标题】:How to push keys (values) with requests module python3?如何使用请求模块 python3 推送键(值)?
【发布时间】:2019-07-14 08:56:38
【问题描述】:

我正在尝试在 amazon.com 的搜索框中添加一些价值。 我使用的是请求而不是硒(按键选项)。 我已经确定了搜索框的 xpath,现在想在其中推送值,即:char“a”或“apple”或任何其他字符串,然后收集结果。 但是,当使用 post 方法推送数据以进行请求时,出现错误。 下面是我的代码:

import requests
from lxml import html
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)''AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
page = requests.get('https://www.amazon.com/', headers=headers)


page = requests.get('https://www.amazon.com/', headers=headers)
response_code = page.status_code
if response_code == 200:
    htmlText = page.text
    tree = html.fromstring(page.content)
    search_box = tree.xpath('//input[@id="twotabsearchtextbox"]')
    pushing_keys = requests.post(search_box,'a')
    print(search_box)

但是我得到了这个错误代码:

requests.exceptions.MissingSchema: Invalid URL "[<InputElement 20b94374a98 name='field-keywords' type='text'>]": No schema supplied. Perhaps you meant http://[<InputElement 20b94374a98 name='field-keywords' type='text'>]?

如何通过请求正确推送搜索框中的任何字符? 谢谢

【问题讨论】:

  • 首先了解更改该搜索框的约定(特定端点)。目前上述方法没有意义

标签: python web-scraping request lxml


【解决方案1】:

尝试使用这种方法:

import requests
base_url = 'https://www.amazon.com'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)''AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
page = requests.get(base_url, headers=headers)
response_code = page.status_code
if response_code == 200:
    key_word_to_search = 'bean bags'
    pushing_keys = requests.get(f'{base_url}/s/ref=nb_sb_noss', headers=headers, params={'k': key_word_to_search})
    print(pushing_keys.content)

搜索框正在使用获取请求。

See here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2016-04-21
    • 2012-04-03
    • 2016-11-27
    相关资源
    最近更新 更多