2016-04-15 105 views
1

我想用Python機械化腳本來自動填寫表單。隨着值根據其他因素而變化。有沒有辦法讓我從文件中讀取?這是我的代碼:Python機械化填充表單與變量或提取文本

br.open('https://url/url) 
br.select_form(nr=0) 
br.form['xxxxx']='123456' 
br.form['yyyyy']='7890' 
br.submit()           
print br.response().read() 

我怎樣才能像

br.form [ 'XXXX'] =開(xxx.txt,R)

所以從xxx.txt讀並填寫表格..不能似乎在網上找到任何東西...

回答

0
br.form['xxxx']=open(xxx.txt,r).read() 

這將給文件的內容直接。

0

如果txt文件只有一行,請嘗試此操作。

br.open(url) 
br.select_form(nr=0) 
with open('example.txt', 'r') as f: 
    for line in f: 
     br.form['xxxxx']= line 
br.form['yyyyy']='7890' 
response = br.submit() 
search = response.read() 

對於另一方面:

br.open(url) 
br.select_form(nr=0) 
lines = [line.rstrip('\n') for line in open('example.txt')] 
br.form['xxxxx']= lines[0] 
br.form['yyyyy']= lines[1] 
response = br.submit() 
search = response.read()