python - Python:find_all 仅适用于某些标签
问题描述
bs4 中的 .findall 函数仅适用于某些 HTML 标记。我正在尝试抓取一个网站。
from bs4 import BeautifulSoup
import requests
url = 'https://bitskins.com/'
page_response = requests.get(url, timeout=5)
page_content = BeautifulSoup(page_response.content, 'html.parser')
# Gather the two lists
skin_list = page_content.find_all('div', attrs={'class': 'panel-heading item-title'})
wear_box = page_content.find_all('div', attrs={'class': 'text-muted text-center'})
当我打印 skin_list 时,它可以成功运行,但是当我尝试打印磨损列表时,它会打印一个空列表。
我尝试了另一件事:
wear_box = page_content.html.search("Wear: {float}")
这带来了一个错误,指出“NoneType”对象不可调用。
我正在使用 Sublime Text 3。
解决方案
from bs4 import BeautifulSoup
import requests
url = 'https://bitskins.com/'
page_response = requests.get(url, timeout=5)
page_content = BeautifulSoup(page_response.content, 'html.parser')
skin_list = page_content.findAll('div', class_ = 'panel item-featured panel-default')
for skin in skin_list:
name = skin.find("div", class_ = "panel-heading item-title")
price = skin.find("span", class_ = "item-price hidden")
discount = skin.find("span", class_ = "badge badge-info")
wear = skin.find("span", class_ = "hidden unwrappable-float-pointer")
print("name:", name.text)
print("Price", price.text)
print("Discount:", discount.text)
# Choose which one you want
for w in wear.text.split(","):
print("Wear:", w)
您试图找到不正确的课程。我添加了一些其他数据,您可以抓取这些数据作为示例。Wear 保存了我输出的一些值。
推荐阅读
- python - TensorFlow-serving 找不到元图 Def
- r - 如何在 ggplot2 中定义 bin?
- java - 如何将支票转换为包含小数位的单词
- java - 服务(事件总线订阅者)未收到来自 Activity 的帖子
- android - onStop() 或 super.onStop() 返回后 Activity 是否可终止
- r - 如何在 R 中编写一个 RegEx 函数来选择具有字母 e 但没有字符串 ee 的国家/地区
- mongodb - Mongoose 模块中 collection.js 中未定义的 Mongoose TypeError '无法读取命名空间'
- java - 不能在 JavaFX 中使用从 ParametreController 传递到 GameController 的初始化(GameController)方法中的对象
- python - Python:在数据框的多列上使用函数
- listview - ListView 不显示更新的列表值。但列确实