2015-05-24 36 views
0

我有一個將POST請求發送到網站的Python代碼,讀取響應並對其進行過濾。對於我使用的POST數據('數字','11111'),它的工作原理非常完美。但是,我想創建一個包含100個不同號碼的txt文件,如1111,2222,3333,4444 ...,然後爲每個請求發送POST請求。你能幫助我如何在Python中做到這一點?在Python中發送多個POST數據

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']

+0

讀取數據'1111,2222,3333,4444文件,..'分裂通過數據上'字符串,'和循環。你嘗試過嗎? –

+0

我想過,但我無法編寫代碼。你能給我一個示例代碼嗎? – user1968957

回答

0

如果您的檔案數據:

「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'] 
+0

它工作完美。非常感謝。 – user1968957

0

1 - 這裏是如何創建一個文件的一個示例:

f = open('test.txt','w') 

這將打開test.txt文件寫入('w')(如果它已經數據,它會被刪除,但如果你想追加它,請寫:f = open('test.txt','a'))或者創建一個,如果它還不存在。請注意,這會在你當前的工作目錄發生,如果你想在一個特定的目錄,與文件名中包含的完整目錄路徑,例如:

f = open('C:\\Python\\test.txt','w') 

2 - 然後寫/追加到該文件中的數據你想,例如:

for i in range(1,101): 
    f.write(str(i*1111)+'\n') 

這會寫100個號碼作爲字符串從1111到111100

3 - 你應該總是關閉文件結尾:

f.close() 

4 - 現在,如果你想從這個文件「test.txt」閱讀:

f = open('C:\\Python\\test.txt','r') 
for i in f: 
print i, 
file.close() 

這是因爲它可以很簡單,

你需要閱讀有關文件I/O在python來自:

https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files

確保您在本文檔選擇正確的Python版本適合你。

0

使用字典可以很容易地處理多個請求。

導入請求

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)