python,mechanize - 用mechanize打开一个文本文件

时间:2013-03-18 22:00:42

标签: python beautifulsoup mechanize

我正在学习机械师。我正在尝试打开一个文本文件,你点击的链接说明了 文字(.prn) 我遇到的一个问题是此页面上只有一个表单,文件不在表单中。 另一个问题是我在这个页面上有几个文本文件,但它们都有相同的名称 文字(.prn)。所以我想我需要到第一个打开它。使我试图打开的文本文件唯一的一件事就是它似乎被命名为

  • 摘要,也许我可以用它来打开它 然后使用br.form.find_control(或者我可以使用:br.click_link,如果我能找到一些方法来指导机械化打开第一个标题为“摘要”的文章

    我所在的网页是: http://www.treasurydirect.gov/govt/reports/pd/mspd/2013/2013_feb.htm

    这里是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">
    

    这是我的代码到目前为止从文本文件所在的页面开始:

           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

    请给我一些帮助,甚至暗示你认为我应该使用什么。

  • 1 个答案:

    答案 0 :(得分:1)

    getstuff=br.click_link(text="Text (.prn)", nr=0)之后,而不是你的 csvData = getstuff.readlines(),你应该致电:

    br.open(getstuff)
    csvData = br.response().read()
    

    如果您需要对上一页执行任何其他操作(例如 2013_feb.htm ),请致电:

    br.back()
    

    会使br回到br.open之前的相同状态。