【发布时间】:2020-03-17 20:48:46
【问题描述】:
我有以下html代码
<table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
<tr>
<td width='41%' align='left'>Title</td>
<td width='10%' align='left'>Year</td>
<td width='10%' align='left'>Price</td>
<table width='99%' border='0' cellpadding='1' class="normal">
<tr>
<td width='41%' align='left'><strong>Quatermass 2</strong></td>
<td width='10%' align='left'>1957</td>
<td width='10%' align='left'>£295</td>
<table width='100%' border='0' cellpadding='0' class='blackbg textheadtitle'>
<tr>
<td width='41%' align='left'>Title</td>
<td width='10%' align='left'>Year</td>
<td width='10%' align='left'>Price</td>
<table width='99%' border='0' cellpadding='1' class="normal">
<tr>
<td width='41%' align='left'><strong>Ghostbusters</strong></td>
<td width='10%' align='left'>1985</td>
<td width='10%' align='left'>£395</td>
我只想抽出岁月
1957
1985
我已经隔离标签是“宽度”:“10%”,我可以在函数中插入一个正则表达式来忽略字符串“年份”,如下所示
from bs4 import BeautifulSoup
import requests
import re
html = ['table.html']
with open("table.html", "r") as f:
contents = f.read()
soup = BeautifulSoup(contents, "lxml")
for name in soup.find_all("td", {"width": "10%"}, string=re.compile(r'^(?!Year$)')):
print((name).get_text())
给我
Price
1957
£295
Price
1985
£395
但是,如果我想忽略字符串 'Price' 和任何包含 '£' 的字符串,我该怎么做呢?我可以输入另一个正则表达式参数吗,也许通过这样做?
for name in soup.find_all("td", {"width": "10%"},string=re.compile(r'^(?!Year$),'),(r'^(?!Price$)'),(r'^(?!£$)')):
print((name).get_text())
非常感谢
【问题讨论】:
标签: python html regex beautifulsoup tags