Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何避免只从HTML代码中提取一部分结果_Python_Beautifulsoup - Fatal编程技术网

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)