gpt4 book ai didi

python-2.7 - 使用Python和Mechanize提交网站html中的数据

转载 作者:行者123 更新时间:2023-12-04 16:20:33 25 4
gpt4 key购买 nike

我有 this网站有四个输入框,分别是Symbol , Expiry Date , From , To .现在我已经编写了一个代码来从 Symbol 中抓取数据和 Expiry Date是这样的:

import requests
import json
from bs4 import BeautifulSoup

r = requests.get("http://www.mcxindia.com/sitepages/BhavCopyCommodityWise.aspx")
soup = BeautifulSoup(r.content)
pop = []
pop_dates = []
count = 0
print soup.prettify()

option_list = soup.findAll("option")
#print option_list

for value in option_list:
#print value
if value.find(text = True):
text = ''.join(value.find(text = True))
text1 = text.encode('ascii')

if count < 32:
pop.append(text1)

while count == 32 or count > 32:
pop_dates.append(text1)
break

count = count + 1

print pop
print pop_dates

所以我想做的是 FromTo我想从我的代码中给出日期,它将接受该输入,在网站的 html 上使用它,并像往常一样在该网站中提供输出。我怎样才能做到这一点??我听说 Mechanize 可以做这些东西,但在这种情况下我怎么能使用 Mechanize ??

最佳答案

你可以尝试这样的事情:

from mechanize import Browser
from bs4 import BeautifulSoup

br = Browser()
br.set_handle_robots( False )
br.addheaders = [('User-agent', 'Firefox')]
br.open("http://www.mcxindia.com/sitepages/BhavCopyCommodityWise.aspx")
br.select_form("form1")
#now enter the dates according to your choice
br.form["mTbFromDate"] = "date-From"
br.form["mTbFromDate"] = "date-To"
response = br.submit()
#now read the response with BeautifulSoup and do whatever you want
soup = BeautifulSoup(response.read())

关于python-2.7 - 使用Python和Mechanize提交网站html中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com