我有一个Python代码,它向网站发送POST请求,读取响应并过滤它。对于我使用的POST数据('number','11111'),它工作得很好。但是,我想创建一个包含100个不同数字的txt文件,如1111222233334444。。。然后发送每个人的POST请求。你能帮我用Python做这个吗?在
import urllib
from bs4 import BeautifulSoup
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Origin': 'http://mahmutesat.com/python.aspx',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'http://mahmutesat.com/python.aspx',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
}
class MyOpener(urllib.FancyURLopener):
version = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'
myopener = MyOpener()
url = 'http://mahmutesat.com/python.aspx'
# first HTTP request without form data
f = myopener.open(url)
soup = BeautifulSoup(f)
# parse and retrieve two vital form values
viewstate = soup.select("#__VIEWSTATE")[0]['value']
eventvalidation = soup.select("#__EVENTVALIDATION")[0]['value']
viewstategenerator = soup.select("#__VIEWSTATEGENERATOR")[0]['value']
formData = (
('__EVENTVALIDATION', eventvalidation),
('__VIEWSTATE', viewstate),
('__VIEWSTATEGENERATOR',viewstategenerator),
('number', '11111'),
('Button', 'Sorgula'),
)
encodedFields = urllib.urlencode(formData)
# second HTTP request with form data
f = myopener.open(url, encodedFields)
soup = BeautifulSoup(f.read())
name=soup.findAll('input',{'id':'name_field'})
for eachname in name:
print eachname['value']
如果文件包含数据:
““示例.txt““
要读取文件内容,可以使用file
^{pr2}$open
操作:现在,将其用作:
1-以下是如何创建文件的示例:
这将打开
^{pr2}$test.txt
文件进行写入('w'
)(如果它已经有数据,它将被删除,但是如果你想附加它,请写:f = open('test.txt','a')
),或者如果它还不存在,则创建一个。请注意,这将发生在当前工作目录中,如果您希望它位于特定目录中,请在文件名中包含完整的目录路径,例如:2-然后将所需的数据写入/附加到此文件,例如:
这将以字符串形式从1111到111100写入100个数字
3-您应该始终在结尾关闭文件:
4-现在如果你想从这个文件“
test.txt
”中读取:这很简单
您需要从以下位置了解python中的文件I/O:
https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files
确保在本文档中为您选择了正确的Python版本。在
使用字典,您可以很容易地处理多个请求
导入请求
相关问题 更多 >
编程相关推荐