【问题标题】:Scrape Data from Website using css style Using Beautifull soup使用 CSS 样式从网站上抓取数据 使用 Beautifulsoup
【发布时间】:2015-05-09 12:56:05
【问题描述】:

我有一个网站,我想从那里刮取优惠券代码。我在这里有两个问题。我在这里使用 python 和美丽的汤。 1)span标签中显示的某些优惠券没有class或id,因此无法从这些标签中获取优惠券。我需要从强标签(AXISCB50)中获取

<h6><span style="color: #808000">25% Cashback on Recharges :</span></h6>
<ul>
<li>Get 25% Cashback upto Rs.25 per transaction.</li>
<li>Coupon Code : <span style="color: #ff0000"><strong>AXISCB50</strong></span></li>
<li>Maximum 2 transaction per Debit/Credit card.</li>
</ul>

是否可以通过指定 style="color: #808000 类似 this(style) 来进行抓取。

2)有些优惠券是通过ajax显示的,只有当我们点击按钮时才会显示。我将如何_这些通过脚本显示的数据?

我第一次进入网络抓取。任何帮助表示赞赏并提前感谢。

【问题讨论】:

    标签: python html python-2.7 beautifulsoup html-parsing


    【解决方案1】:

    要获得优惠券代码,我不会依赖颜色样式属性。相反,将 next element 获取到 Coupon Code 文本

    soup.find(text=lambda x: x and x.startswith('Coupon Code')).next_element.text
    

    演示:

    >>> from bs4 import BeautifulSoup
    >>> 
    >>> data = """
    ... <h6><span style="color: #808000">25% Cashback on Recharges :</span></h6>
    ... <ul>
    ... <li>Get 25% Cashback upto Rs.25 per transaction.</li>
    ... <li>Coupon Code : <span style="color: #ff0000"><strong>AXISCB50</strong></span></li>
    ... <li>Maximum 2 transaction per Debit/Credit card.</li>
    ... </ul>
    ... """
    >>> 
    >>> soup = BeautifulSoup(data)
    >>> 
    >>> print soup.find(text=lambda x: x.startswith('Coupon Code')).next_element.text
    AXISCB50
    

    有些优惠券是通过 ajax 显示的,它只显示一次 单击按钮。我将如何抓取_这些数据显示在 脚本?

    您需要研究单击按钮时发送的请求。使用浏览器开发者工具,网络选项卡。然后,在您的 python 代码中模拟请求。 requests 通常是一个不错的选择。

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2019-12-15
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多