用漂亮的汤提取网页内容

时间:2019-06-05 02:17:10

标签: html python-3.x web-scraping beautifulsoup

我正在尝试使用带有python 3的漂亮汤从电子商务网站上刮掉一件商品的价格。我还需要做些什么才能从第一次拉动中提取价格?

我尝试了其他代码组合,但是对这种方法没有很深入的了解。

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.walmart.com/ip/GoGreen-Power-6-Outlet-Surge-Protector-16103MS-2-5-cord-White/46097919')
soup = BeautifulSoup(page.text, 'html.parser')

price_hide = soup.find(class_='price-characteristic')
print(price_hide)

wprice = price_hide.find_all(content)
print(wprice)

第一个打印功能有效

<span class="price-characteristic" content="3.98" itemprop="price">3</span>

第二个不是很多。

我希望能印出3.98的内容价格

1 个答案:

答案 0 :(得分:0)

比您想象的要容易,请尝试以下代码:

from bs4 import BeautifulSoup
import requests

page = requests.get('https://www.walmart.com/ip/GoGreen-Power-6-Outlet-Surge-Protector-16103MS-2-5-cord-White/46097919')
soup = BeautifulSoup(page.text, 'html.parser')

price_hide = soup.find(class_='price-characteristic')
print(price_hide)

wprice = price_hide["content"]
print(wprice)

我希望这会有所帮助!