如何使用请求模块python3推送键(值)?

时间:2019-07-14 08:56:38

标签: python web-scraping request lxml

我正在尝试在amazon.com的搜索框中添加一些值。 我使用的是请求而不是硒(“推键”选项)。 我已经确定了搜索框的xpath,现在想在其中推送值,即IE: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'>]?

如何正确在搜索框中输入带有请求的字符? 谢谢

1 个答案:

答案 0 :(得分: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