[英]python, mechanize - open a text file with mechanize
I am learning mechanzie. 我正在学习机械师。 I am trying to open a text file , the link that you would click on says
我正在尝试打开一个文本文件,您将单击的链接说
Text (.prn) One problem i am having is there is only 1 form on this page and the file is not in the form. 文本(.prn)我遇到的一个问题是此页面上只有1个表单,并且文件不在表单中。 Another problem for me is there are a couple Text files on this page, but they all have the same name Text (.prn).
对我来说,另一个问题是此页面上有几个文本文件,但是它们都具有相同的名称文本(.prn)。 So i guess i need to get to the first one and open it.
所以我想我需要去第一个并且打开它。 One thing that makes the text file I am trying to open unique is that it seems to be named
使我尝试打开的文本文件唯一的一件事是它似乎被命名为
The webpage I am on is: http://www.treasurydirect.gov/govt/reports/pd/mspd/2013/2013_feb.htm 我使用的网页是: http : //www.treasurydirect.gov/govt/reports/pd/mspd/2013/2013_feb.htm
here is the section of the html where the text file is i want to open in mechanize: 这是我要在机械化中打开文本文件的html部分:
</div> <!-- END LOCALNAV --> <!-- BEGIN CONTENT --> <div id="content"> <h1>February 2013</h1> <!-- InstanceBeginEditable name="content" --> <ul> <li>Summary <ul> <li><a href="/govt/reports/pd/mspd/2013/opds022013.pdf">Adobe Acrobat (.pdf)</a></li> <li><a href="/govt/reports/pd/mspd/2013/opds022013.prn">Text (.prn)</a></li> </ul> </li> <li>STRIPS <ul> <li><a href="/govt/reports/pd/mspd/2013/opdr022013.pdf">Adobe Acrobat (.pdf)</a></li> <li><a href="/govt/reports/pd/mspd/2013/opdr022013.xls">Excel 5.0/95 (.xls )</a></li> <li><a href="/govt/reports/pd/mspd/2013/opdr022013.prn">Text (.prn)</a></li> </ul> </li> <li>Entire MSPD <ul> <li><a href="/govt/reports/pd/mspd/2013/opdx022013.xls">Excel File for Primary Dealers</a></li> <li><a href="/govt/reports/pd/mspd/2013/opdm022013.pdf">Adobe Acrobat (.pdf)</a></li> <li><a href="/govt/reports/pd/mspd/2013/opdm022013.xls">Excel 5.0/95 (.xls)</a></li> <li><a href="/govt/reports/pd/mspd/2013/opdm022013.prn">Text (.prn)</a></li> </ul> </li> </ul> <p>Note: To read or print a PDF document, you need the Adobe Acrobat Reader (v5.0 or higher) software installed on your computer. You can download the Adobe Acrobat Reader from the <a href="/exit.htm?http://get.adobe.com/reader/">Adobe website</a>.</p> <p>Note: If you need <a href="/helpdownload.htm">help downloading...</a></p> <!-- InstanceEndEditable --> </div> <!-- END CONTENT --> <!-- BEGIN SUBLOCALNAV --> <div id="right">
here is my code so far starting on the page before the one the text file is on: 这是到目前为止我的代码从文本文件所在的页面之前的页面开始:
br = mechanize.Browser() br.set_handle_equiv(False) br.open(site) print 'br.title',br.title() allforms = list(br.forms()) br.form = allforms[0] br.follow_link(text_regex="February", nr=0) #br.click_link(text='February', nr=0) # this works to #next page print br.title() allforms = list(br.forms()) print allforms br.form = allforms[0] getstuff=br.click_link(text="Text (.prn)", nr=0) # this works to csvData=getstuff.readlines() # this is where is get error
here is my traceback: 这是我的回溯:
Traceback (most recent call last): File "treasury2.py", line 56, in <module> csvData=getstuff.readlines() File "C:\\Python27\\lib\\site-packages\\mechanize\\_urllib2_fork.py", line 173, in __getattr__ raise AttributeError, attr AttributeError: readlines
I am using mechanize , BeautifulSoup ,urllib , urllib2 and python27 我正在使用机械化,BeautifulSoup,urllib,urllib2和python27
Please give me some help or even a hint on what you think i should use to use. 请给我一些帮助,甚至提示我应该使用什么。
Right after getstuff=br.click_link(text="Text (.prn)", nr=0)
, instead of your csvData=getstuff.readlines() , you should call: 在
getstuff=br.click_link(text="Text (.prn)", nr=0)
,而不是csvData = getstuff.readlines() ,您应该调用:
br.open(getstuff)
csvData = br.response().read()
In case you need to do anything else with the previous page (ie, 2013_feb.htm ), call: 如果您需要对上一页(即2013_feb.htm )进行其他操作,请致电:
br.back()
which will bring br
back to the same state as right before the br.open
. 这将使
br
返回到与br.open
之前相同的状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.