使用Python和Mechanize在网站的html中提交数据

时间:2014-06-04 08:27:45

标签: python-2.7 mechanize

我有this网站,有四个输入框SymbolExpiry DateFromTo。现在我编写了一个代码来清除SymbolExpiry 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并给出该网站的常规输出。我怎样才能做到这一点??我听说机械化可以做这些东西,但我怎么能在这种情况下使用机械化?

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情:

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())