本文介绍了Python 请求输入搜索按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 html,我需要在其中输入一些文本值.我正在尝试使用请求模块使用以下 python 代码,但是它没有按预期工作.有人可以帮忙吗.
原始html:
<input autocomplete="off" class="LM6RPg" name="q" placeholder="搜索产品、品牌等" title="搜索产品、品牌等" type="text" value=""/>
预期的 html 发布:
<input autocomplete="off" class="LM6RPg" name="q" placeholder="搜索产品、品牌等" title="搜索产品、品牌等" type="text" value="mobiles"/>
Python 代码:
导入请求r=requests.get("https://www.flipkart.com/", verify=False)search=r.find('input' , value="")搜索=移动"表单={"":搜索}mobile=requests.post("https://www.flipkart.com/", data=form, verify=False)
解决方案
导入请求从 bs4 导入 BeautifulSoupquery = str(input("你想搜索哪个产品\n>"))参数 = {'q':查询,'otracker': '搜索','otracker1': '搜索','市场': 'FLIPKART','as-show': 'on','作为':'关闭'}r = requests.get("https://www.flipkart.com/search", params=params)打印(r.url)汤 = BeautifulSoup(r.text, 'html.parser')# 打印(soup.prettify())
I have below html where I need to input some text in value. I am trying with below python code using requests module, however it is not working as expected. Can someone please help.
Original html:
<div class="O8ZS_U">
<input autocomplete="off" class="LM6RPg" name="q" placeholder="Search for products, brands and more" title="Search for products, brands and more" type="text" value=""/>
</div>
Expected html to post:
<div class="O8ZS_U">
<input autocomplete="off" class="LM6RPg" name="q" placeholder="Search for products, brands and more" title="Search for products, brands and more" type="text" value="mobiles"/>
</div>
Python code:
import requests
r=requests.get("https://www.flipkart.com/", verify=False)
search=r.find('input' , value="")
search="mobile"
form={"" : search}
mobile=requests.post("https://www.flipkart.com/", data=form, verify=False)
解决方案
import requests
from bs4 import BeautifulSoup
query = str(input("Which Product You Would Like To Search For\n> "))
params = {
'q': query,
'otracker': 'search',
'otracker1': 'search',
'marketplace': 'FLIPKART',
'as-show': 'on',
'as': 'off'
}
r = requests.get("https://www.flipkart.com/search", params=params)
print(r.url)
soup = BeautifulSoup(r.text, 'html.parser')
# print(soup.prettify())
这篇关于Python 请求输入搜索按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!