如何使用BeautifulSoup从Python中删除字符串中的html标记(How to remove html tags from strings in Python using Beautifu

编程入门 行业动态 更新时间:2024-10-18 21:17:02
如何使用BeautifulSoup从Python中删除字符串中的html标记(How to remove html tags from strings in Python using BeautifulSoup)

在这里编程新手:)

我想使用BeautifulSoup从网站打印价格。 这是我的代码:

#!/usr/bin/env python # -*- coding: utf-8 -*- from bs4 import BeautifulSoup, SoupStrainer from urllib2 import urlopen url = "Some retailer's url" html = urlopen(url).read() product = SoupStrainer('span',{'style': 'color:red;'}) soup = BeautifulSoup(html, parse_only=product) print soup.prettify()

它按以下顺序打印价格:

<span style="color:red;"> 180 </span> <span style="color:red;"> 1250 </span> <span style="color:red;"> 380 </span>

我试过print soup.text.strip()但它返回1801250380

请帮我打印每行的价格:)

非常感谢!

programming newbie here :)

I'd like to print the prices from the website using BeautifulSoup. this is my code:

#!/usr/bin/env python # -*- coding: utf-8 -*- from bs4 import BeautifulSoup, SoupStrainer from urllib2 import urlopen url = "Some retailer's url" html = urlopen(url).read() product = SoupStrainer('span',{'style': 'color:red;'}) soup = BeautifulSoup(html, parse_only=product) print soup.prettify()

and it prints prices in the following order:

<span style="color:red;"> 180 </span> <span style="color:red;"> 1250 </span> <span style="color:red;"> 380 </span>

I tried print soup.text.strip() but it returned 1801250380

Please help me to print the prices per single row :)

Many thanks!

最满意答案

>>> print "\n".join([p.get_text(strip=True) for p in soup.find_all(product)]) 180 1250 380 >>> print "\n".join([p.get_text(strip=True) for p in soup.find_all(product)]) 180 1250 380

更多推荐

本文发布于:2023-08-06 22:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何使用   标记   Python   BeautifulSoup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!