【问题标题】:Python BeautifulSoup - Scrape Web Content Inside IframesPython BeautifulSoup - 在 iframe 中抓取 Web 内容
【发布时间】:2019-06-28 13:27:44
【问题描述】:

我们有这个网址: 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%

我们如何提取它?

【问题讨论】:

  • 我认为您很可能需要模拟浏览器,例如 selenium 并使用 selenium.switch_to.frame(),因为 iframe 的内容不会在请求中加载,并且指向该 iframe 的链接很可能不会除非会话保持,否则工作。

标签: python web-scraping beautifulsoup


【解决方案1】:

只需要获取iframesrc属性,然后请求解析其内容即可:

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("\t".join([e.text for e in row.select("th, td")]))

结果:

反馈 1 个月 3 个月 6 个月 正面(4-5 星) 154 562 1,550 中性(3 星)8 19 65 负面(1-2 星)8 20 57 正反馈率 95.1% 96.6% 96.5%

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多