【问题标题】:extract iFrame content using BeautifulSoup使用 BeautifulSoup 提取 iFrame 内容
【发布时间】:2017-03-03 23:02:40
【问题描述】:

在下面的页面上 --> link,我正在尝试使用BeautifulSoup 来提取最底部的<a> 文本,即'Private Life''Lost Boy'

但我很难抓取<iframe> 的内容。

我了解到它需要来自浏览器的不同请求。

所以我试过了:

iframexx = soup.find_all('iframe')
for iframe in iframexx:
    try:
        response = urllib2.urlopen(iframe)
        results = BeautifulSoup(response)
        print results

但返回None

如何解析下面的 html,以便获取每个 a['href'].get_text()

【问题讨论】:

    标签: python html iframe beautifulsoup


    【解决方案1】:

    浏览器将在单独的请求中加载 iframe 内容,因此您需要获取 iframe src 中存在的 URL。如果需要,您可以使用 selenium,或者直接抓取数据本身。 这是一个例子:

    import requests
    import re
    
    url = 'https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/310079005&color=ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false'
    
    response = requests.get(url)
    
    Artist = re.search(b'(?<=artist":")(.*?)(?=")', response.content).group(0).decode("utf-8")
    Song = re.search(b'(?<=title":")(.*?)(?=")', response.content).group(0).decode("utf-8")
    
    print ("%s - %s" % (Artist, Song))
    

    私人生活 - 迷路的男孩

    【讨论】:

    • 你如何仅使用 python 脚本获取 url?
    • @TheMightyNight 看起来 Zroq 手动将 iframe src 中的 url 放入其 url 变量中。例如,他们没有以编程方式获取它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多