2024-09-27 07:20:51 发布
网友
一个网页包含我需要检索的产品代码,它位于以下HTML部分:
<table...> <tr> <td> <font size="2">Product Code#</font> <br> <font size="1">2342343</font> </td> </tr> </table>
所以我想最好的方法是首先引用文本值为'productcode#'的html元素,然后在TD中引用第二个字体标记。在
有什么想法?在
我的策略是:
<font>
代码:
from BeautifulSoup import BeautifulSoup html = open("products.html").read() soup = BeautifulSoup(html) product_codes = [tag.parent.findNextSiblings('font')[0].contents[0] for tag in soup.findAll(text='Product Code#')]
您可以使用此regex(或类似的):
<td>\n\ <font\ size="2">Product\ Code\#</font>\n\ <br>\n\ <font\ size="1">(?<ProductCode>.+?)</font>\n\ </td>
你可以根据你的RegExp引擎删除一些转义。。。我当时很谨慎。在
假设soup是您的BeautifulSoup实例:
soup
BeautifulSoup
int(''.join(soup("font", size="1")[0](text=True)))
或者,如果需要获取多个产品代码:
我的策略是:
<font>
并找到父元素的下一个同级<font>
元素代码:
您可以使用此regex(或类似的):
<td>\n\ <font\ size="2">Product\ Code\#</font>\n\ <br>\n\ <font\ size="1">(?<ProductCode>.+?)</font>\n\ </td>
你可以根据你的RegExp引擎删除一些转义。。。我当时很谨慎。在
假设
soup
是您的BeautifulSoup
实例:或者,如果需要获取多个产品代码:
^{pr2}$相关问题 更多 >
编程相关推荐