Python 如何避免只从HTML代码中提取一部分结果
我有一个从链接中刮取数据的脚本。我有以下结果集:Python 如何避免只从HTML代码中提取一部分结果,python,beautifulsoup,Python,Beautifulsoup,我有一个从链接中刮取数据的脚本。我有以下结果集: 查找所有img元素,这些元素在src属性内具有“flags”,并且位于p元素内,具有class=“flag”,然后从src属性值中提取语言值: import re from bs4 import BeautifulSoup pattern = re.compile(r"/(\w+)\.gif$") for img in soup.select("p.flag img[src*=flags]"): matc
查找所有img
元素,这些元素在src
属性内具有“flags”,并且位于p
元素内,具有class=“flag”
,然后从src
属性值中提取语言值:
import re
from bs4 import BeautifulSoup
pattern = re.compile(r"/(\w+)\.gif$")
for img in soup.select("p.flag img[src*=flags]"):
match = pattern.search(img["src"])
if match:
print(match.group(1))
re.findall(r'src=".+?/(\w+)\.gif"', text)