如何使用请求模块 python3 推送键(值)?
How to push keys (values) with requests module python3?
我正在尝试在 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'>]?
如何使用请求正确地推送搜索框中的任何字符?
谢谢
尝试使用这种方法:
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
我正在尝试在 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'>]?
如何使用请求正确地推送搜索框中的任何字符? 谢谢
尝试使用这种方法:
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