Python的BeautifulSoup - 刮削Web内容iframe内

问题描述 投票:1回答:1

我们有这个网址:https://www.aliexpress.com/store/feedback-score/1665279.html

而需要的内容是“反馈记录”表,这是一个iframe内:

Feedback    1 Month 3 Months    6 Months
Positive (4-5 Stars)    154 562 1,550
Neutral (3 Stars)   8   19  65
Negative (1-2 Stars)    8   20  57
Positive feedback rate  95.1%   96.6%   96.5%

我们如何解压呢?

python web-scraping beautifulsoup
1个回答
3
投票

你只需要获得srciframe属性,然后请求和解析它的内容:

import requests
from bs4 import BeautifulSoup

s = requests.Session()
r = s.get("https://angrygoldfish.aliexpress.com/store/feedback-score/1665279.html")

soup = BeautifulSoup(r.content, "html.parser")
iframe_src = soup.select_one("#detail-displayer").attrs["src"]

r = s.get(f"https:{iframe_src}")

soup = BeautifulSoup(r.content, "html.parser")
for row in soup.select(".history-tb tr"):
    print("\t".join([e.text for e in row.select("th, td")]))

结果:

Feedback        1 Month         3 Months        6 Months
Positive (4-5 Stars)    154     562     1,550
Neutral (3 Stars)       8       19      65
Negative (1-2 Stars)    8       20      57
Positive feedback rate  95.1%   96.6%   96.5%
© www.soinside.com 2019 - 2024. All rights reserved.