分析表中的链接 [英] Parsing table for a link

查看:231
本文介绍了分析表中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够在Python 2.7隔离用美丽的汤在一个HTML表中的一行。是一个学习的经验,但很高兴得到那么远。不幸的是我有点停留在这个在下位。

我需要得到下面的选择文件汇款报告我XLS格式输入链接。因为这可以改变出现的顺序,它需要是动态的。我不知道如何找到输入,然后抓住它后面的链接。

我一直在尝试一些的findAll和nextSibling方法,但我使用Python和美丽的汤经验不足抱着我。该BeautifulSoup文档伟大的,但去了一下我的头。


 < TR类=奇>
 &所述; TD标题=C1>
  报告下载
 < / TD>
 &所述; TD标题=C2>
  <输入ARIA标签=选择报告格式PDFID =documentChkBx0NAME =documentChkBx类型=复选框值=5446/>
  < A HREF =?/ A / document.html键= 5446>
   < IMG ALT =便携文档格式SRC =/ IMG /图标/ icon_PDF.gif>
   < / IMG>
  &所述; / A>
  <输入ARIA标签=选择报告格式XLSID =documentChkBx1NAME =documentChkBx类型=复选框值=5447/>
  < A HREF =?/ A / document.html键= 5447>
   < IMG ALT =Excel的小号preadsheet格式SRC =/ IMG /图标/ icon_XLS.gif>
   < / IMG>
  &所述; / A>
 < / TD>
 &所述; TD标题=C4>
  2015年4月27日
 < / TD>
 &所述; TD标题=C5>
  2015年5月26日
 < / TD>
 &所述; TD标题=C6>
  2015年5月26日美东时间上午10:00
 < / TD>
< / TR>


解决方案

通过检查找到输入 ARIA标签属性,并获得<一个href=\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-next-siblings-and-find-next-sibling\"相对=nofollow>以下 A 同级元素:

 标签= soup.find(输入{咏叹调标签:选择报告格式XLS})
链接= label.find_next_sibling(A中,href = TRUE)的href]

I've been able to isolate a row in a html table using Beautiful Soup in Python 2.7. Been a learning experience, but happy to get that far. Unfortunately I'm a bit stuck on this next bit.

I need to get the link that follows the "Select document Remittance Report I format XLS" input. As this can change order of appearance, it needs to be dynamic. I'm not sure how to find that input and then grab the link that follows it.

I've been trying some findAll and nextSibling methods but my inexperience with python and beautiful soup is holding me back. The BeautifulSoup documentation is great but going a bit over my head.


<tr class="odd">
 <td header="c1">
  Report Download
 </td>
 <td header="c2">
  <input aria-label="Select Report format PDF" id="documentChkBx0" name="documentChkBx" type="checkbox" value="5446"/>
  <a href="/a/document.html?key=5446">
   <img alt="Portable Document Format" src="/img/icons/icon_PDF.gif">
   </img>
  </a>
  <input aria-label="Select Report format XLS" id="documentChkBx1" name="documentChkBx" type="checkbox" value="5447"/>
  <a href="/a/document.html?key=5447">
   <img alt="Excel Spreadsheet Format" src="/img/icons/icon_XLS.gif">
   </img>
  </a>
 </td>
 <td header="c4">
  04/27/2015
 </td>
 <td header="c5">
  05/26/2015
 </td>
 <td header="c6">
  05/26/2015 10:00AM EDT
 </td>
</tr>

解决方案

Locate the input by checking aria-label attribute and get the following a sibling element:

label = soup.find("input", {"aria-label": "Select Report format XLS"})
link = label.find_next_sibling("a", href=True)["href"]

这篇关于分析表中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆