- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是值
我正在尝试抓取一个网站,并将网站中的一些数据解析为可用的格式以进行分析。我想要提取的数据在 <div>
内 block ,这样我就可以轻松访问 HTML 的该部分。
我基本上想得到一个Python列表字典,如下所示:
{"Processor": ["3.7GHz Quad-Core Intel Xeon E5 processor with 10MB of L3 cache"],
"Memory": ["16GB (four 4GB) of 1866MHz DDR3 EEC"],
"Storage": ["512GB PCIe-based flash storage"],
"Input/Output": ["Four USB 3 ports (up to 5 Gbps)", "Six Thunderbolt 2 ports (up to 20 Gbps)"]}
但是,我尝试解析的 HTML 数据看起来更像是这样的:
<div class="as-productinfosection-panel TechSpecs-panel row">
<div class="as-productinfosection-sidepanel column large-3 small-12">
<h3 data-autom="sectionTitle">Tech Specs</h3>
</div>
<div class="as-productinfosection-mainpanel column large-9 small-12">
<h4 class="h4-para-title">
Processor
</h4>
<div class="para-list as-pdp-lastparalist">
<p>
3.7GHz Quad-Core Intel Xeon E5 processor with 10MB of L3 cache
</p>
</div>
<h4 class="h4-para-title">
Memory
</h4>
<div class="para-list as-pdp-lastparalist">
<p>
16GB (four 4GB) of 1866MHz DDR3 EEC
</p>
</div>
<h4 class="h4-para-title">
Storage
</h4>
<div class="para-list as-pdp-lastparalist">
<p>
512GB PCIe-based flash storage<sup>1</sup>
</p>
</div>
<h4 class="h4-para-title">
Graphics
</h4>
<div class="para-list as-pdp-lastparalist">
<p>
Dual AMD FirePro D300 graphics processors with 2GB of GDDR5 VRAM each
</p>
</div>
<h4 class="h4-para-title">
Input/Output
</h4>
<div class="para-list">
<p>
Four USB 3 ports (up to 5 Gbps)
</p>
</div>
<div class="para-list">
<p>
Six Thunderbolt 2 ports (up to 20 Gbps)
</p>
</div>
<div class="para-list">
<p>
Dual Gigabit Ethernet ports
</p>
</div>
<div class="para-list as-pdp-lastparalist">
<p>
One HDMI 1.4 Ultra HD port
</p>
</div>
<h4 class="h4-para-title">
Audio
</h4>
<div class="para-list">
<p>
Combined optical digital audio output/analog line out minijack
</p>
</div>
<div class="para-list">
<p>
Headphone minijack with headset support
</p>
</div>
<div class="para-list">
<p>
HDMI port supports multichannel audio output
</p>
</div>
<div class="para-list as-pdp-lastparalist">
<p>
Built-in speaker
</p>
</div>
<h4 class="h4-para-title">
Wireless
</h4>
<div class="para-list">
<p>
802.11ac Wi-Fi wireless networking<sup>2</sup>
</p>
</div>
<div class="para-list">
<p>
IEEE 802.11a/b/g/n compatible
</p>
</div>
<div class="para-list as-pdp-lastparalist">
<p>
Bluetooth 4.0 wireless technology
</p>
</div>
<h4 class="h4-para-title">
Size and weight
</h4>
<div class="para-list">
<p>
Height: 9.9 inches (25.1 cm)
</p>
</div>
<div class="para-list">
<p>
Diameter: 6.6 inches (16.7 cm)
</p>
</div>
<div class="para-list as-pdp-lastparalist">
<p>
Weight: 11 pounds (5 kg)<sup>3</sup>
</p>
</div>
</div>
</div>
我知道我可以使用类似的方法从该部分中提取所有标题:
r = requests.get('https://www.apple.com/shop/product/G0PK0LL/A/refurbished-mac-pro-37ghz-quad-core-intel-xeon-e5?fnode=3bb458bfb26c0f3137a9899791eba511037c7868c96bc9c236e2eeb016997c327ef9487f5e5bb8f13fb21e31d7a35da45e091125611c8e6c01a06b814f51596e3f2786b2d3f60262d4dd50a008f5acc8')
soup = bs(r.content, "html.parser")
section = soup.find("div",{"class":"as-productinfosection-panel TechSpecs-panel row"})
section.findAll('h4')
我可以找到这样的所有段落:
section.findAll('p')
但我不知道如何基本上遍历 <div>
一点一点地保持标题与下一个标题之前的以下信息同步。
最佳答案
使用 find_next_siblings()
的解决方案收集属于某个类别的所有 para-list
元素:
from collections import defaultdict
import requests
from bs4 import BeautifulSoup
url = "https://www.apple.com/shop/product/G0PK0LL/A/refurbished-mac-pro-37ghz-quad-core-intel-xeon-e5?fnode=3bb458bfb26c0f3137a9899791eba511037c7868c96bc9c236e2eeb016997c327ef9487f5e5bb8f13fb21e31d7a35da45e091125611c8e6c01a06b814f51596e3f2786b2d3f60262d4dd50a008f5acc8"
soup = BeautifulSoup(requests.get(url).content, "html.parser")
section = soup.find("div", {"class": "as-productinfosection-panel TechSpecs-panel row"})
d = defaultdict(list)
for cat in section.select(".h4-para-title"):
k = cat.text.strip()
for item in cat.find_next_siblings():
if "para-list" not in item.attrs["class"]:
break
else:
d[k].append(item.text.strip())
print(dict(d))
结果:
{
'Processor': ['3.7GHz Quad-Core Intel Xeon E5 processor with 10MB of L3 cache'],
'Memory': ['16GB (four 4GB) of 1866MHz DDR3 EEC'],
'Storage': ['512GB PCIe-based flash storage1'],
'Graphics': ['Dual AMD FirePro D300 graphics processors with 2GB of GDDR5 VRAM each'],
'Input/Output': ['Four USB 3 ports (up to 5 Gbps)', 'Six Thunderbolt 2 ports (up to 20 Gbps)', 'Dual Gigabit Ethernet ports', 'One HDMI 1.4 Ultra HD port'],
'Audio': ['Combined optical digital audio output/analog line out minijack', 'Headphone minijack with headset support', 'HDMI port supports multichannel audio output', 'Built-in speaker'],
'Wireless': ['802.11ac Wi-Fi wireless networking2', 'IEEE 802.11a/b/g/n compatible', 'Bluetooth 4.0 wireless technology'],
'Size and weight': ['Height: 9.9 inches (25.1 cm)', 'Diameter: 6.6 inches (16.7 cm)', 'Weight: 11 pounds(5 kg)3']
}
关于python - BeautifulSoup 将 HTML 解析为字典,其中 <h> 是键,<p> 是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54499112/
HTML 在 div 中包含字符串: 'div class="slide"' 'img src="xttps://site.com/files/r_1000,kljg894/43k5j/35h43jk
我用这个方法 allcity = dom.body.findAll(attrs={'id' : re.compile("\d{1,2}")}) 返回这样的列表: [掳虏驴碌路驴碌脴虏煤脨脜脧垄脥酶 隆
我正在使用 Jupyter 笔记本、Python 3.5 和虚拟环境。 在我的虚拟环境中,我做了: (venv) > pip install BeautifulSoup4 这似乎运行良好 b/c 终端
我打算用 GUI 制作一个字典程序,但我在第一个障碍上就失败了。我刚刚安装了一个模块( PyDictionary ),但是当我运行以下代码时出现错误。 from PyDictionary import
我正在将一些解析器从 BeautifulSoup3 迁移到 BeautifulSoup4,我认为考虑到 lxml 非常快并且它是我在 BS4 中使用的解析器,分析它会变得多快是个好主意,这里是分析结果
这个问题在这里已经有了答案: Getting Python error "from: can't read /var/mail/Bio" (6 个答案) 关闭 11 个月前。 From Beauti
目前我无法输入这个,因为根据 top,我的处理器是 100%,我的内存是 85.7%,都被 python 占用了。 为什么?因为我让它通过一个 250 兆的文件来删除标记。 250兆,就是这样!我一直
我写了下面的代码: from bs4 import BeautifulSoup import sys # where is the sys module in the source code fold
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我正在为一个项目使用 BeautifulSoup。这是我的 HTML 结构 John Sam Bailey Jack
这段代码正确地从我的博客中提取了马拉地语文本。我很欣赏使用漂亮的汤和正则表达式是多么容易。 from bs4 import BeautifulSoup import requests, re url
我想获取 HTML 中隐藏输入字段的值。 我想用 Python 编写一个正则表达式,它将返回 fooId 的值,前提是我知道 HTML 中的行遵循以下格式 有人可以提供一个 Python 示例来解
我正在尝试获取特定月份的所有链接、标题和日期,例如网站上的三月,我正在使用 BeautifulSoup 这样做: from bs4 import BeautifulSoup import reques
我正试图通过此链接收集有关 2020 年世界上收入最高的运动员收入的信息 https://www.forbes.com/profile/roger-federer/?list=athletes这是第一
我正在尝试从带有美丽汤的网页中捕获所有相关链接。我需要的所有链接都有 class="btn btn-gray"还有文字 More Info<> 仅提取这些链接的最佳方法是什么? 最佳答案 这个怎么样?
我正在尝试抓取一个具有下拉菜单的站点,用户可以在其中选择要显示的数据的年份。但是,我似乎被困在我的实现中。 这是网站网址:https://www.pgatour.com/tournaments/mas
我正在使用 BeautifulSoup 和 mechanise 从网页中查找一些内容。问题是有时找不到我正在寻找的字符串。我不知道有什么问题 对于许多网页,它可以正常工作数月,但突然停止工作。然后我必
( 更新代码 就在下面) 我有一个类:UrlData , 生成一个 url 列表: for url in urls: rawMechSiteInfo = mech.open(url) #me
这是我到目前为止所做的事情: import requests from bs4 import BeautifulSoup URL = "https://www.google.com/search?q=
我是一名优秀的程序员,十分优秀!