python - 从一长串中获取所有组
问题描述
我有以下字符串:
aaa<a class="c-item_foot" href="/news/a/">11r11</a></div>bbb<a class="c-item_foot" href="/news/b/">222</a></div>ccgc<a class="c-item_foot" href="/news/c/">3333a333</a></div>ddd<a class="c-item_foot" href="/news/d/">44a444444</a></div>eee
我尝试从这一行获取以下值:
- 11r11
- 222
- 3333a333
- 44a444444
换句话说,要获得 和 之间的<a class="c-item_foot" href="/news/*/">
值</a></div>
。我试图通过以下方式获得它:
text=open("./string.txt","r").read()
print(u'\n'.join(re.findall(r"<a class=\"c-item_foot.*>(.*)</a></div>", text)))
但只能拿到最后一组44a444444
。谁能告诉我正确的例子?
解决方案
我建议你使用像 BeautifulSoup 这样的 html 解析库。
html_doc = 'aaa<a class="c-item_foot" href="/news/a/">11r11</a></div>bbb<a class="c-item_foot" href="/news/b/">222</a></div>ccgc<a class="c-item_foot" href="/news/c/">3333a333</a></div>ddd<a class="c-item_foot" href="/news/d/">44a444444</a></div>eee'
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
values = [tag.text for tag in soup.find_all('a')]
推荐阅读
- php - file_put_contents(C:\Users\FNRI\Desktop):无法打开流:没有这样的文件或目录
- c - 函数不能返回returnSize数组中的元素的问题
- python - 错误:“无法使用无作为查询值”尝试在 Django 中使用 ListView 分页时
- sql - 如何在 sql oracle 中插入子类型值?
- swift - youtube-ios-player-helper-swift 上的 UIWebView 到 WKWebView
- javascript - 添加新数据时自动刷新另一个页面 Laravel 5.8 Ajax
- mysql - 我已在数据库中插入数据但不知道如何在 laravel 中显示
- node.js - 使用nodejs一段时间后如何动态更新数据?
- html - 定位后引导列重叠
- wordpress - wordpress 中的 wp-includes 是什么以及它的用途?