在 Python 中发送多个 POST 数据
Sending multiple POST data in Python
我有一个 Python 代码,可以向网站发送 POST 请求,读取响应并对其进行过滤。对于我使用的 POST 数据 ('number', '11111') 并且它工作完美。但是,我想创建一个包含 100 个不同数字的 txt 文件,如 1111,2222,3333,4444...,然后为每个数字发送 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']
1 - 以下是有关如何创建文件的示例:
f = open('test.txt','w')
这将打开 test.txt
文件进行写入 ('w'
)(如果它已经有数据,它将被删除,但如果你想追加它,请写入:f = open('test.txt','a')
)如果尚不存在,则创建一个。请注意,这将发生在您当前的工作目录中,如果您希望它位于特定目录中,请在文件名中包含完整的目录路径,例如:
f = open('C:\Python\test.txt','w')
2 - 然后 write/append 到这个文件你想要的数据,例如:
for i in range(1,101):
f.write(str(i*1111)+'\n')
这将从 1111 到 111100 的 100 个数字写入字符串
3 - 您应该始终在最后关闭文件:
f.close()
4 - 现在如果你想从这个文件 'test.txt
' 中读取:
f = open('C:\Python\test.txt','r')
for i in f:
print i,
file.close()
这很简单,
您需要阅读 python 中的文件 I/O 来自:
https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files
确保您 select 是本文档中适合您的 Python 版本。
如果您的文件有数据:
"sample.txt"
1111,2222,3333,4444,5555,6666,7777,8888,......(and so on)
读取文件内容,可以使用文件open
操作:
import itertools
#open the file for read
with open("sample.txt", "r") as fp:
values = fp.readlines()
#Get the values split with ","
data = [map(int, line.split(",")) for line in values]
numbers = list(itertools.chain(*data)) #Ensuring if its having many lines then concatenate
现在,将其用作:
for number in numbers:
formData = (
('__EVENTVALIDATION', eventvalidation),
('__VIEWSTATE', viewstate),
('__VIEWSTATEGENERATOR',viewstategenerator),
('number', str(number)), # Here you use the number obtained
('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']
使用字典,您可以非常轻松地处理多个请求。
导入请求
values = {
'__EVENTVALIDATION': event_validation,
'__LASTFOCUS': '',
'__VIEWSTATE': view_state,
'__VIEWSTATEGENERATOR': '6264FB8D',
'ctl00$ContentPlaceHolder1$ButGet': 'Get Report',
'ctl00$ContentPlaceHolder1$Ddl_Circles': 'All Circles',
'ctl00$ContentPlaceHolder1$Ddl_Divisions': '-- Select --',
'ctl00$ContentPlaceHolder1$TxtTin': tin_num,
'ctl00$ContentPlaceHolder1$dropact': 'all'
}
headers_1 = {
'Origin': 'https://www.apct.gov.in',
'User-Agent': user_agent,
'Cookie': cookie_1,
'Accept-Encoding': 'gzip, deflate, br',
'Referer': url_1,
'Content-Type': 'application/x-www-form-urlencoded',
'Upgrade-Insecure-Requests': '1'
}
try:
req = requests.post(url_1, data=values, headers=headers_1)
我有一个 Python 代码,可以向网站发送 POST 请求,读取响应并对其进行过滤。对于我使用的 POST 数据 ('number', '11111') 并且它工作完美。但是,我想创建一个包含 100 个不同数字的 txt 文件,如 1111,2222,3333,4444...,然后为每个数字发送 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']
1 - 以下是有关如何创建文件的示例:
f = open('test.txt','w')
这将打开 test.txt
文件进行写入 ('w'
)(如果它已经有数据,它将被删除,但如果你想追加它,请写入:f = open('test.txt','a')
)如果尚不存在,则创建一个。请注意,这将发生在您当前的工作目录中,如果您希望它位于特定目录中,请在文件名中包含完整的目录路径,例如:
f = open('C:\Python\test.txt','w')
2 - 然后 write/append 到这个文件你想要的数据,例如:
for i in range(1,101):
f.write(str(i*1111)+'\n')
这将从 1111 到 111100 的 100 个数字写入字符串
3 - 您应该始终在最后关闭文件:
f.close()
4 - 现在如果你想从这个文件 'test.txt
' 中读取:
f = open('C:\Python\test.txt','r')
for i in f:
print i,
file.close()
这很简单,
您需要阅读 python 中的文件 I/O 来自:
https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files
确保您 select 是本文档中适合您的 Python 版本。
如果您的文件有数据:
"sample.txt"
1111,2222,3333,4444,5555,6666,7777,8888,......(and so on)
读取文件内容,可以使用文件open
操作:
import itertools
#open the file for read
with open("sample.txt", "r") as fp:
values = fp.readlines()
#Get the values split with ","
data = [map(int, line.split(",")) for line in values]
numbers = list(itertools.chain(*data)) #Ensuring if its having many lines then concatenate
现在,将其用作:
for number in numbers:
formData = (
('__EVENTVALIDATION', eventvalidation),
('__VIEWSTATE', viewstate),
('__VIEWSTATEGENERATOR',viewstategenerator),
('number', str(number)), # Here you use the number obtained
('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']
使用字典,您可以非常轻松地处理多个请求。
导入请求
values = {
'__EVENTVALIDATION': event_validation,
'__LASTFOCUS': '',
'__VIEWSTATE': view_state,
'__VIEWSTATEGENERATOR': '6264FB8D',
'ctl00$ContentPlaceHolder1$ButGet': 'Get Report',
'ctl00$ContentPlaceHolder1$Ddl_Circles': 'All Circles',
'ctl00$ContentPlaceHolder1$Ddl_Divisions': '-- Select --',
'ctl00$ContentPlaceHolder1$TxtTin': tin_num,
'ctl00$ContentPlaceHolder1$dropact': 'all'
}
headers_1 = {
'Origin': 'https://www.apct.gov.in',
'User-Agent': user_agent,
'Cookie': cookie_1,
'Accept-Encoding': 'gzip, deflate, br',
'Referer': url_1,
'Content-Type': 'application/x-www-form-urlencoded',
'Upgrade-Insecure-Requests': '1'
}
try:
req = requests.post(url_1, data=values, headers=headers_1)