use*_*208 0 html python regex html-table
我正在尝试使用正则表达式匹配表中的单元格,但问题不是所有单元格都遵循相同的模式.例如,td可以采用以下格式:
<td><a href="page101010.html">PageNumber</a></td>
Run Code Online (Sandbox Code Playgroud)
或这种格式:
<td align="left" ></td>
Run Code Online (Sandbox Code Playgroud)
基本上,td中的超链接部分并不存在,只是在某些部分.
我尝试使用下面的python正则表达式代码匹配这种情况,但它失败了.
match = re.search(r'<td align="left" ><?a?.+\>?(.+)\<?\/?a?\>?\<\/td\>', tdlink)
Run Code Online (Sandbox Code Playgroud)
我只需要'匹配'来找到上面()中包含的部分.但是我收到语法错误或无对象消息.
我哪里错了?
您正在使用正则表达式,并且将XML与此类表达式匹配变得太复杂,太快.
使用HTML解析器,Python有几个可供选择:
ElementTree示例:
from xml.etree import ElementTree
tree = ElementTree.parse('filename.html')
for elem in tree.findall('tr'):
print ElementTree.tostring(elem)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
350 次 |
| 最近记录: |