2013-07-15 77 views
1

我使用Python 2.7,机械化和beautifulsoup,如果它可以帮助我可以使用的urllib与蟒蛇下载zip文件机械化

好吧,我想下载都在不同的HTML表格一对夫妇不同的zip文件。我知道特定的文件是什么表中(我知道他们是在第一,第二,第三......表)
这里是从网页的HTML格式的第二个表:

<table class="fe-form" cellpadding="0" cellspacing="0" border="0" width="50%"> 
      <tr> 
       <td colspan="2"><h2>Eligibility List</h2></td> 
      </tr> 


      <tr> 
       <td><b>Eligibility File for Met-Ed</b> - 
       <a href="/content/fecorp/supplierservices/eligibility_list.suppliereligibility.html?id=ME&ftype=1&fname=cmb_me_elig_lst_06_2013.zip">cmb_me_elig_lst_06_2013.zip</td> 
      </tr> 



      <tr> 
       <td><b>Eligibility File for Penelec</b> - 
       <a href="/content/fecorp/supplierservices/eligibility_list.suppliereligibility.html?id=PN&ftype=1&fname=cmb_pn_elig_lst_06_2013.zip">cmb_pn_elig_lst_06_2013.zip</td> 
      </tr> 



      <tr> 
       <td><b>Eligibility File for Penn Power</b> - 
       <a href="/content/fecorp/supplierservices/eligibility_list.suppliereligibility.html?id=PP&ftype=1&fname=cmb_pennelig_06_2013.zip">cmb_pennelig_06_2013.zip</td> 
      </tr> 



      <tr> 
       <td><b>Eligibility File for West Penn Power</b> - 
       <a href="/content/fecorp/supplierservices/eligibility_list.suppliereligibility.html?id=WP&ftype=1&fname=cmb_wp_elig_lst_06_2013.zip">cmb_wp_elig_lst_06_2013.zip</td> 
      </tr> 


      <tr> 
       <td>&nbsp;</td> 
      </tr> 
     </table> 

我打算用下面的代码只是为了让到第2表:

from bs4 import BeautifulSoup 
html= br.response().read() 
soup = BeautifulSoup(html) 
table = soup.find("table", class=fe-form) 

我猜那类=“FE-表”是错误的,因为它不会工作,但也有没有表的其它属性区别于其他表格。所有表都有cellpadding =“0”cellspacing =“0”border =“0”width =“50%”。我想我不能使用find()函数。

所以我试图去第二个表,然后下载这个页面上的文件。有人能给我一些信息来推动我朝着正确的方向前进。我以前使用过表格,但没有使用表格。我希望有一些方法来找到找到zip文件的特定标题我要找然后下载它们,因为我永远知道他们的名字

感谢您的帮助, 汤姆

+0

这个问题呢?我想用python机械化下载一个zip文件。该zip文件不是以表格形式存在的。任何人都可以给我提示,学习如何做到这一点?我一直在谷歌搜索有关使用Python机械化浏览表的信息,找不到任何东西。我在正确的轨道上吗? – user1087809

回答

0

要选择表你只需要做

table = soup.find('table', attrs={'class' : 'fe-form', 'cellpadding' : '0' }) 

这假设在你的文档里只有一个class = fe-form和cellpadding = 0的表。如果还有更多,这段代码将只选择第一个表。可以肯定你是不是在页面上俯瞰东西,你可以做

tables = soup.findAll('table', attrs={'class' : 'fe-form', 'cellpadding' : '0' }) 
table = tables[0] 

也许断言,LEN(表)== 1,以确保只有一个表。

现在,要下载文件,你可以做很多事情。从您的代码已装入mechanize假设,你可以像

a_tags = table.findAll('a') 

for a in a_tags: 
    if '.zip' in a.get('href'): 
    br.retrieve(a.get('href'), a.text) 

这将所有文件下载到您当前的工作目录,并会根据自己的链接文本为它们命名。

+0

@ user1087809:这个回答有用吗?还是没有工作? – djas

+0

我刚刚没有在这个网页上,谢谢你的帮助djas – user1087809