python,机械化-使用机械化打开文本文件 [英] python, mechanize - open a text file with mechanize
问题描述
我正在学习机械师.我正在尝试打开一个文本文件,您将单击的链接显示为
文字(.prn)
我遇到的一个问题是此页面上只有1个表单,并且文件不在表单中.
对我来说,另一个问题是此页面上有几个文本文件,但是它们的名称相同
文字(.prn).所以我想我需要去第一个并且打开它.使我尝试打开的文本文件唯一的一件事是,它似乎被命名为
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.
Another problem for me is there are a couple Text files on this page, but they all have the same name
Text (.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
我访问的网页是: http://www.treasurydirect.gov/govt/reports/pd /mspd/2013/2013_feb.htm
这是我要在机械化中打开文本文件的html部分:
here is the section of the html where the text file is i want to open in mechanize:
</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
这是我的回溯:
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
我正在使用mechanize,BeautifulSoup,urllib,urllib2和python27
I am using mechanize , BeautifulSoup ,urllib , urllib2 and python27
请给我一些帮助,甚至就您认为我应该使用的内容提供提示.
Please give me some help or even a hint on what you think i should use to use.
推荐答案
在getstuff=br.click_link(text="Text (.prn)", nr=0)
之后,而不是您的 csvData = getstuff.readlines(),您应该调用:
Right after getstuff=br.click_link(text="Text (.prn)", nr=0)
, instead of your csvData=getstuff.readlines(), you should call:
br.open(getstuff)
csvData = br.response().read()
如果您需要对上一页进行其他操作(即 2013_feb.htm ),请致电:
In case you need to do anything else with the previous page (i.e., 2013_feb.htm), call:
br.back()
,它将使br
返回到与br.open
之前相同的状态.
which will bring br
back to the same state as right before the br.open
.
这篇关于python,机械化-使用机械化打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!