我正在尝试解析此页面:https://www.ldlc.com/fr-be/informatique/pieces-informatique/carte-professionnelle/c4685/
问题是,在这个元素中:https://gyazo.com/e544be64a41a121bdb0c0f71aef50692,我想要包含价格的div。如果你检查页面,你可以看到这个部分的html代码,显示如下:
<div class="price">
<div class"price">
"thePrice"
<sup>93</sup>
</div>
</div>
但是,当使用page_soup = soup(my_html_page, 'html.parser')
、page_soup = soup(my_html_page, 'lxml')
或page_soup = soup(my_html_page, 'html5lib')
时,我只得到该部分的结果:
<div class="price"></div>
就是这样。我在互联网上搜索了几个小时,想弄清楚为什么内部div没有被解析。
三个不同的解析器,如果这是问题的话,似乎都没有通过内部子级与其父级共享相同类名的事实。
希望它能帮助你。
from bs4 import BeautifulSoup
import requests
url = 'https://www.ldlc.com/fr-be/informatique/pieces-informatique/carte-professionnelle/c4685/'
html = BeautifulSoup(requests.get(url).content, 'html.parser')
prices = html.find_all("div", {"class": "price"})
for price in prices:
print(price.text)
打印输出
561€95
169€94
165€95
1 165€94
7 599€95
267€95
259€94
599€95
511€94
1 042€94
2 572€94
783€95
2 479€94
2 699€95
499€94
386€95
169€94
2 343€95
783€95
499€94
499€94
259€94
267€95
165€95
169€94
2 399€95
561€95
2 699€95
2 699€95
6 059€95
7 589€95
10 991€95
9 619€94
2 479€94
3 135€95
7 589€95
511€94
1 042€94
386€95
599€95
1 165€94
2 572€94
783€95
2 479€94
2 699€95
499€94
169€94
2 343€95
2 699€95
3 135€95
6 816€95
7 589€95
561€95
267€95
要刮取class="price">
中的所有价格,请参见以下示例:
import requests
from bs4 import BeautifulSoup
url = 'https://www.ldlc.com/fr-be/informatique/pieces-informatique/carte-professionnelle/c4685/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
# Select all the 'price' classes
for tag in soup.select('div.price'):
print(tag.text)