Python 如何使用BeautifulSoup从一段html代码中获取价值?
我刚开始使用python进行一些网页抓取,BeautifulSoup似乎到处都被推荐 我有如下内容:Python 如何使用BeautifulSoup从一段html代码中获取价值?,python,html,beautifulsoup,Python,Html,Beautifulsoup,我刚开始使用python进行一些网页抓取,BeautifulSoup似乎到处都被推荐 我有如下内容: <table class="table with-row-highlight table-archive"> <tbody> <tr> <td> <div class="user-tagline "> <span class="username " data-avatar
<table class="table with-row-highlight table-archive">
<tbody>
<tr>
<td>
<div class="user-tagline ">
<span class="username " data-avatar="aaaaaaa">player1</span>
<span class="user-rating">(1357)</span>
<span class="country-flag-small flag-113" tip="Portugal"></span>
</div>
<div class="user-tagline ">
<span class="username " data-avatar="bbbbbbb">player2</span>
<span class="user-rating">(1387)</span>
<span class="country-flag-small flag-70" tip="Indonesia"></span>
</div>
</td>
<td>
<a class="clickable-link text-middle" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">
<div class="pull-left">
<span class="game-result">1</span>
<span class="game-result">0</span>
</div>
<div class="result">
<i class="icon-square-minus loss" tip="Lost"></i>
</div>
</a>
</td>
<td class="text-center">
<a class="clickable-link" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">30 min</a>
</td>
<td class="text-right">
<a class="clickable-link text-middle moves" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">25</a>
</td>
<td class="text-right miniboard">
<a class="clickable-link archive-date" href="https://www.chess.com/live/game/2249663029?username=belemnarmada" target="_self">Aug 9, 2017</a>
</td>
</tr>
100 <tr></tr> here
</tbody>
</table>
玩家1
(1357)
玩家2
(1387)
这里是100
我的代码到此为止,如何编写python代码来循环所有
对,并提取每个
对中每个
对的所有类
编辑
我想可能我没有在这里解释清楚,你的代码返回的是HTML中的类名,而我要寻找的是相应的值,例如,有一个类
用户名
,我想得到它的值player1
和player2
;有一个类country flag small
flag-70
我想得到tip=Indonesia
这应该可以做到:
import requests
from bs4 import BeautifulSoup
res = requests.get('someLink')
soup = BeautifulSoup(res.text)
classes = []
for element in soup.find_all(class_=True):
classes.extend(element["class"])
print(classes)
我使用您的html文件对此进行了测试,结果如下:
['table', 'with-row-highlight', 'table-archive', 'user-tagline', 'username', 'user-rating', 'country-flag-small', 'flag-113', 'user-tagline', 'username', 'user-rating', 'country-flag-small','flag-70', 'clickable-link', 'text-middle', 'pull-left', 'game-result', 'game-result', 'result', 'icon-square-minus', 'loss', 'text-center', 'clickable-link', 'text-right', 'clickable-link', 'text-middle', 'moves', 'text-right', 'miniboard', 'clickable-link', 'archive-date']
请注意,如果尚未安装pip3,则必须执行pip3安装请求
此外,如果要使用计算机上的文件测试此功能,可以执行以下操作:
from bs4 import BeautifulSoup
file = open('/path/To/Your/HtmlFile.html', 'r')
lines = file.read()
soup = BeautifulSoup(lines)
classes = []
for element in soup.find_all(class_=True):
classes.extend(element["class"])
print(classes)
请问你的密码在哪里?你已经成为会员7年了,所以你知道它是如何调试问题的。请问你在这里多久了?难道你不会像其他人一样在这里提供任何建设性的回答,而不是对你不认识的人刻薄吗?在我看来这并不粗鲁,但如果有人冒犯我,我道歉。我要密码。很明显,对于寻求调试帮助的问题,我们希望这样做。但是如果有帮助,请查看和。谢谢Jane的回答,尝试在此处复制结果,但在此处看到一些意外错误:我现在可以在Windows中运行您的代码,非常感谢,如果有更多问题,将在此处更新。我想可能我没有在此处解释清楚,您的代码返回的是该HTML中的类名,而我要查找的是相应的值,例如,有一个类“username”
,我想得到它的值“player1和player2
;有一个类“country flag small flag-70”
我想得到tip=“Indonesia”