1

我正在学习机械。我正在尝试打开一个文本文件,您单击的链接显示
文本 (.prn) 我遇到的一个问题是此页面上只有 1 个表单,并且该文件不在表单中。我的另一个问题是此页面上有几个文本文件,但它们都具有相同的名称文本 (.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

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

  • 4

    1 回答 1

    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

    于 2013-03-18T23:04:28.493 回答