python, mechanize - open a text file with mechanize

喜夏-厌秋 提交于 2019-12-13 18:04:19

问题


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

  • Summary , maybe i can use this to open it and then use br.form.find_control( or maybe i can use: br.click_link , if i can find some way to direct mechanize to open the first one titled "Summary"

    The webpage I am on is: 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:

    </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

    Please give me some help or even a hint on what you think i should use to use.


    回答1:


    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()
    

    In case you need to do anything else with the previous page (i.e., 2013_feb.htm), call:

    br.back()
    

    which will bring br back to the same state as right before the br.open.



    来源:https://stackoverflow.com/questions/15487772/python-mechanize-open-a-text-file-with-mechanize

  • 易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!