2015-05-29 79 views
6

我正嘗試使用Mechanize在此page上提交一個表單。使用python修復網頁的字符編碼機械化

br.open("http://mspc.bii.a-star.edu.sg/tankp/run_depth.html") 
#selecting form to fill 
br.select_form(nr = 0) 
#input for the form 
br['pdb_id'] = '1atp' 
req = br.submit() 

然而,這提供了以下錯誤

mechanize._form.ParseError: expected name token at '<! INPUT PDB FILE>\n\t' 

我想,這是因爲一些錯位的字符編碼(ref)。我想知道如何解決這個問題。

回答

2

你的問題是一些中斷HTML comment tags,導致一個無效的網站機械化的解析器無法讀取。但你可以代替use the included BeautifulSoup parser,這在我的情況下工作(Python 2.7.9,機械化0.2.5):

#!/usr/bin/env python 
#-*- coding: utf-8 -*- 
import mechanize 

br = mechanize.Browser(factory=mechanize.RobustFactory()) 
br.open('http://mspc.bii.a-star.edu.sg/tankp/run_depth.html') 
br.select_form(nr=0) 
br['pdb_id'] = '1atp' 
response = br.submit()