python - 如何在 BeautifulSoup 中使用 find() 和 find_all()?
问题描述
我目前正在做一些网络抓取。我有这个 HTML:
<meta property="og:price:amount" content="1.89"/>
<meta property="og:price:standard_amount" content="6.31"/>
<meta property="og:price:currency" content="USD"/>
我正在使用美丽的汤(Python)。
我要提取的信息是 1.89 和 6.31(产品价格)。
这是我的代码:
import requests
from bs4 import BeautifulSoup
page = requests.get('https://spanish.alibaba.com/product-detail/crazy-hot-selling-multifunctional-battery-powered-360-degree-rotation-led-light-makeup-mirror-60769168637.html?spm=a2700.8270666-66.2016122619262.17.5a4d5d09En8wm9')
# Create a BeautifulSoup object
soup = BeautifulSoup(page.text, 'html.parser')
#print(soup.get_text())
# get the repo list
v2 = soup.find_all("meta", {"property": "og:price:amount", "content": True}['content'] )
print("v2 is",v2)
错误在.find_all()
函数中,我不确定如何提取数据。我也试过这个.find()
功能
这是我得到的关于美丽汤功能如何工作的信息:
Signature: find_all(name, attrs, recursive, string, limit, **kwargs)
帮我配置一下 .find()
功能。谢谢!
解决方案
而不是find_all()
仅仅使用find()
find_all()
返回元素列表。
v2 = soup.find("meta", {"property": "og:price:amount", "content": True})['content']
print("v2 is",v2)
或者您可以使用Css 选择器:
v2 = soup.select_one('meta[property="og:price:amount"][content]')['content']
print("v2 is",v2)
推荐阅读
- sql - 我需要找到每个公司的最高薪水高于平均薪水的员工
- javascript - 为什么 new Array(3) == ",,"; 返回真
- amazon-web-services - 我们可以允许 codepipeline 仅将更新的文件存储到 S3 吗?
- ruby-on-rails - form_with 中的模型
- java - Java 运行时相当于使用 javax Processor / google @AutoService 进行注释处理
- r - GGPLOT2 如何在每个 log10 步骤上获得标签?
- google-cloud-dataflow - Apache Beam Stateful DoFn 周期性输出所有 K/V 对
- php - WooCommerce:添加到订单列表中的每个订单列
- angular - i18n angular.json 的输出路径
- c++ - 为具有相同名称的函数反转生成的 vtable 函数顺序