【问题标题】:Website Scraper [closed]网站刮板[关闭]
【发布时间】:2015-02-18 21:34:46
【问题描述】:

是否可以取回值

data-price="4.7056"

来自以下html

 '<tr style="cursor:pointer;" class="percent_price_list price_list_content" data-count="500" data-price="4.7056">
                <td class="quick_list_count">500k</td>
                <td>£4.71</td>
                <td>£23.53</td>
            </tr>'

如果是这样,我应该怎么做,我知道如何在两个 html 标签之间正常抓取信息,但不知道如何获取上面提到的内容。我知道漂亮的汤库,这在这种情况下有用吗?

【问题讨论】:

  • 我需要进一步了解您使用的语言、平台或框架。如果 Javascript 可用,那么简单的 element.getAttribute('data-price') 就可以了,但不知何故,我认为您没有使用 Javascript。

标签: python web-scraping


【解决方案1】:

BeautifulSoup 是一个 HTML 解析器。以下内容应该可以帮助您入门:

from bs4 import BeautifulSoup

html = """<tr style="cursor:pointer;" class="percent_price_list price_list_content" data-count="500" data-price="4.7056">
                <td class="quick_list_count">500k</td>
                <td>£4.71</td>
                <td>£23.53</td>
            </tr>"""
soup = BeautifulSoup(html)
for item in soup.find_all('tr', {'data-price': True}):
    print(item['data-price'])

# 4.7056

【讨论】:

    【解决方案2】:

    是的,BeautifulSoup 可以做到这一点,并在 this answer 中进行了介绍。你试图通过它的声音来获取标签的价值。

    另一个问题的片段:

    print soup.find('span', {"class":"thisClass"})['title']

    编辑:抱歉代码格式,这是移动应用程序的问题。

    【讨论】:

      猜你喜欢
      • 2012-03-05
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多