【问题标题】:Python BeautifulSoup parsingPython BeautifulSoup 解析
【发布时间】:2012-01-26 06:19:32
【问题描述】:

我正在尝试抓取一些内容(我对 Python 非常陌生),但遇到了一个绊脚石。我要抓取的代码是:

<h2><a href="/best-sellers/sj-b9822.html">Spear & Jackson Predator Universal Hardpoint Saw     - 22"</a></h2>
<p><span class="productlist_mostwanted_rrp">    
Was: <span class="strikethrough">£12.52</span></span><span class="productlist_mostwanted_save">Save: £6.57(52%)</span></p>

<div class="clear"></div>

<p class="productlist_mostwanted_price">Now: £5.95</p>

我要抓取的是链接文本(Spear & Jackson 等)和价格(5.95 英镑)。我在 Google、BeautifulSoup 文档和这个论坛上进行了查看,我设法使用以下代码提取了“现在:5.95 英镑”:

for node in soup.findAll('p', { "class" : "productlist_grid_price" }):
     print ''.join(node.findAll(text=True))

但是我追求的结果只是 5.95。我尝试使用以下方法获取链接文本(Spear & Jackson)也取得了有限的成功:

soup.h2.a.contents[0]

当然,这只会返回第一个结果。

我的最终目标是让结果看起来像:

Spear & Jackson Predator Universal Hardpoint Saw - 22 5.95
etc
etc

当我希望将其导出到 csv 时,我需要弄清楚如何将数据放入 2 列中。就像我说我对 python 很陌生,所以我希望这是有道理的。

感谢您的帮助!

非常感谢

【问题讨论】:

    标签: python screen-scraping beautifulsoup


    【解决方案1】:

    我认为您正在寻找的是这样的:

    from BeautifulSoup import BeautifulSoup
    import re
    
    soup = BeautifulSoup(open('prueba.html').read())
    item = re.sub('\s+', ' ', soup.h2.a.text)
    price = soup.find('p', {'class': 'productlist_mostwanted_price'}).text
    price = re.search('\d+\.\d+', price).group(0)
    
    print item, price
    

    示例输出:

    Spear & Jackson Predator 通用硬点锯 - 22" 5.95

    请注意,对于商品,正则表达式仅用于删除多余的空格,而对于价格,则用于捕获数字。

    【讨论】:

    • 谢谢!这抓住了第一个。您是否介意告诉我如何构造一个循环以返回所有结果,因为这段代码只返回第一个,尽管很完美。
    • 这取决于它们在 DOM 中的位置(如果它们在同一页面中)。
    • 是的,它们在同一个页面中。我尝试过创建一个循环,但我的努力只值得鄙视。总的来说,编程还是很新的!
    • 谁能帮我循环这个,让它返回所有结果而不是第一个?谢谢
    • 正如我所说,它们取决于 DOM 上的位置。请提供缺失的信息,或者只是提出一个新问题作为此问题的后续。
    【解决方案2】:
    html = '''
    <h2><a href="/best-sellers/sj-b9822.html">Spear & Jackson Predator Universal Hardpoint Saw     - 22</a></h2>
    <p><span class="productlist_mostwanted_rrp">    
    Was: <span class="strikethrough">&pound;12.52</span></span><span class="productlist_mostwanted_save">Save: &pound;6.57(52%)</span></p>
    <div class="clear"></div>
    <p class="productlist_mostwanted_price">Now: &pound;5.95</p>
    '''
    
    from BeautifulSoup import BeautifulSoup
    import re
    
    soup = BeautifulSoup(html)
    desc = soup.h2.a.getText()
    price_str = soup.find('p', {"class": "productlist_mostwanted_price" }).getText()
    price = float(re.search(r'[0-9.]+', price_str).group())
    
    print desc, price
    

    【讨论】:

    • 谢谢,但是它的价格不正确(比如 5.0)并且不会循环播放。不过谢谢
    • @PeterStannett 真的吗?通过 Python2.7 运行它会得到Spear &amp; Jackson Predator Universal Hardpoint Saw - 22 5.95。您是否遇到过剪切粘贴问题?
    • 问题完全出在我身上。道歉!我以复制和粘贴的形式重新运行它,而不是我输入它,这很好! [0-9] 是否意味着如果产品价格为 16 英镑,它就不会打印?我也在努力让代码做不止一个。非常感谢您的帮助。
    • 谁能帮我尝试构建一个循环以返回页面中的所有结果?我已经尝试过,但正在努力让它发挥作用!谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-02-06
    • 2011-05-03
    • 2014-03-06
    • 2014-06-16
    • 2011-07-21
    • 2017-09-12
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多