Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.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 使用BeautifulSoup4从HTML中提取字段_Python_Html_Beautifulsoup - Fatal编程技术网

Python 使用BeautifulSoup4从HTML中提取字段

Python 使用BeautifulSoup4从HTML中提取字段,python,html,beautifulsoup,Python,Html,Beautifulsoup,我是第一次使用BeautifulSoup4,但遇到了一些必须直截了当的问题。我有一个元素标记,看起来像这样: <td class="stage" data-value="phase3">\n \n Phase 3\n<svg height="5" viewbox="1 1 95 5" width="95" xmlns="http://www.w3.org/2000/svg">\n<g f

我是第一次使用BeautifulSoup4,但遇到了一些必须直截了当的问题。我有一个元素标记,看起来像这样:

<td class="stage" data-value="phase3">\n                                    
\n    Phase 3\n<svg height="5" viewbox="1 1 95 5" width="95" 
xmlns="http://www.w3.org/2000/svg">\n<g fill="none" transform="translate(1 1 
)">\n<rect fill="#911C36" height="5" rx="2" width="15"></rect>\n<rect 
fill="#D6A960" height="5" rx="2" width="15" x="16"></rect>\n<rect 
fill="#E7DE6F" height="5" rx="2" width="15" x="32"></rect>\n<rect fill="#ddd" 
height="5" rx="2" width="15" x="48"></rect>\n<rect fill="#ddd" height="5" 
rx="2" width="15" x="64"></rect>\n<rect fill="#ddd" height="5" rx="2" 
width="15" x="80"></rect>\n</g>\n</svg> </td>
正确的查询是什么?

指定传递的
True
与任何值都匹配。像这样的方法应该会奏效:

from bs4 import BeautifulSoup


soup = BeautifulSoup("""
<td class="stage" data-value="phase3">\n                                    
\n    Phase 3\n<svg height="5" viewbox="1 1 95 5" width="95" 
xmlns="http://www.w3.org/2000/svg">\n<g fill="none" transform="translate(1 1 
)">\n<rect fill="#911C36" height="5" rx="2" width="15"></rect>\n<rect 
fill="#D6A960" height="5" rx="2" width="15" x="16"></rect>\n<rect 
fill="#E7DE6F" height="5" rx="2" width="15" x="32"></rect>\n<rect fill="#ddd" 
height="5" rx="2" width="15" x="48"></rect>\n<rect fill="#ddd" height="5" 
rx="2" width="15" x="64"></rect>\n<rect fill="#ddd" height="5" rx="2" 
width="15" x="80"></rect>\n</g>\n</svg> </td>
""", "html.parser")

colors = [x["fill"] for x in soup.findAll("rect", {"fill": True})]
data_vals = [x["data-value"] for x in soup.findAll("td", {"data-value": True})]

print(colors)
print(data_vals)

谢谢,这真的很有帮助。我想为自己澄清一下:这段代码作为一个独立的代码运行良好,但在我的表解析器中,我首先查找'tr'标记,然后在给定的'tr'中查找所有'td'列,比如columns=tr.findAll('td'),然后我才尝试将代码应用到列[2],如上所述。所以我不是在一个BeautifulSoup对象上做findAll,而是在一个bs4.element.Tag对象上做findAll。然后突然findAll找不到任何属性,“数据值”查询生成一个空列表,而“颜色”仍然有效。你有什么建议可以解释为什么会发生这种情况吗?我不能不看你的代码就说出来,但是如果你有一个单独的元素,你可以像dict一样访问这个属性:
fill\u elems=soup.findAll(True,{“fill”:True})print(fill\u elems[2][“fill”])
输出:
\D6A960
。请记住,
findAll()
返回一个
ResultSet
对象。
from bs4 import BeautifulSoup


soup = BeautifulSoup("""
<td class="stage" data-value="phase3">\n                                    
\n    Phase 3\n<svg height="5" viewbox="1 1 95 5" width="95" 
xmlns="http://www.w3.org/2000/svg">\n<g fill="none" transform="translate(1 1 
)">\n<rect fill="#911C36" height="5" rx="2" width="15"></rect>\n<rect 
fill="#D6A960" height="5" rx="2" width="15" x="16"></rect>\n<rect 
fill="#E7DE6F" height="5" rx="2" width="15" x="32"></rect>\n<rect fill="#ddd" 
height="5" rx="2" width="15" x="48"></rect>\n<rect fill="#ddd" height="5" 
rx="2" width="15" x="64"></rect>\n<rect fill="#ddd" height="5" rx="2" 
width="15" x="80"></rect>\n</g>\n</svg> </td>
""", "html.parser")

colors = [x["fill"] for x in soup.findAll("rect", {"fill": True})]
data_vals = [x["data-value"] for x in soup.findAll("td", {"data-value": True})]

print(colors)
print(data_vals)
['#911C36', '#D6A960', '#E7DE6F', '#ddd', '#ddd', '#ddd']
['phase3']