python - 使用python进行网页抓取以打印类div
问题描述
我想将它们全部打印为顶部 div 中给定站点的一类 div。这将是我有兴趣打印的网站 html 的一部分
<div class="game">
<div class="history-feed__collection">
<div class="history-feed__card h-card h-card_sm h-card_spades" style="width: 41px; margin-right: 18px; opacity: 1;">
<div class="h-card__sign">9</div></div>
<div class="history-feed__card h-card h-card_sm h-card_hearts" style="width: 41px; margin-right: 18px; opacity: 1;">
<div class="h-card__sign">K</div></div>
<div class="history-feed__card h-card h-card_sm h-card_diamonds" style="width: 41px; margin-right: 18px; opacity: 1;">
<div class="h-card__sign">Q</div></div>
<div class="history-feed__card h-card h-card_sm h-card_clubs" style="width: 41px; margin-right: 18px; opacity: 1;">
<div class="h-card__sign">2</div>
</div></div>
Eu gostaria que o programa imprimisse assim: "history-feed__card h-card h-card_sm h-card_spades, history-feed__card h-card h-card_sm h-card_hearts, ..."
我启动了这段代码,但我仍然发现问题,因为代码只打印 Div 中包含的内容,而不是其类的名称
from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:\chromedriver')
driver.get('https://card.com')
id = driver.find_elements_by_xpath('//*[@class]')
for ii in id:
print(ii.get_attribute('class="hilo-history-feed__collection"'))
driver.close()
解决方案
我设法用这段代码取得了成功
import requests
from bs4 import BeautifulSoup
URL = 'http://www.card.com'
response = requests.get(URL)
soup = BeautifulSoup(response.content, 'html5lib')
for i in soup.find_all('div'):
print(i)
感谢所有帮助过的人
推荐阅读
- linux - 我正在尝试编写一个 shell 脚本来从文件中读取用户名,但它不起作用。我正在发布我正在编写和输出的脚本
- android - Android,可以在 XML 中添加片段,但不能通过代码
- cmake - Eclipse C/C++ lldb 调试器设置 macOS Catalina
- php - 关于表单提交值未在 php 中插入数据库
- apache-nifi - 安排 NiFi 处理器在收到当天的第一个流文件时运行
- angular - 在 onkeyup 函数期间显示错误
- javascript - 从网页调用 java 方法以生成处理草图并在网页上显示此草图
- twitter-bootstrap - Vee-validate 3 和 Bootstrap 3 表单错误样式
- r - read.table:标题中的百分号 (%) 和正斜杠 (/) 替换为点 (.)
- intellij-idea - Intellij IDEA GIT 注释