Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

web scraping - Python BeautifulSoup - Scrape Web Content Inside Iframes

We have this URL: https://www.aliexpress.com/store/feedback-score/1665279.html

And the needed content is the "Feedback History" table, which is inside an 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%

How do we extract it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You just need to obtain the src attribute of the iframe, and then request and parse its content:

import requests
from bs4 import BeautifulSoup

s = requests.Session()
r = s.get("https://www.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("".join([e.text for e in row.select("th, td")]))

Result:

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%

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...