我正在使用python学习Rest API。并使用html页面https://adminlte.io/themes/AdminLTE/pages/examples/profile.html#
如何使用json对象从此处检索所有用户?
尝试以下代码:
from bs4 import BeautifulSoup
content = '''
<span class="username">
<a href="#">Jonathan Burke Jr.</a>
<a href="#" class="pull-right btn-box-tool"><i class="fa fa-times"></i></a>
</span>
'''
soup = BeautifulSoup(content, 'html.parser')
spans = soup.find_all("span", {"class": "username"})
for span in spans:
links = span.find_all('a')
for link in links:
print(link['href'])
还在提到的链接中检查了一个用户名 它显示
<span class="username">
<a href="#">Jonathan Burke Jr.</a>
<a href="#" class="pull-right btn-box-tool"><i class="fa fa-times"></i></a>
</span>attrs["username"])
答案 0 :(得分:-2)
请记住,您要执行的操作与REST或API无关。
也就是说,您可以使用一个名为BeautifulSoup的简洁python模块,该模块可用于解析HTML页面。
编辑
尝试一下
import requests
from bs4 import BeautifulSoup
r = requests.get("https://adminlte.io/themes/AdminLTE/pages/examples/profile.html")
soup = BeautifulSoup(r.text, 'html.parser')
name_box = soup.find('span', attrs={'class': 'username'})
print(name_box)
这有帮助吗?
**编辑2 **
如果您想查找所有条目,请使用
import requests
from bs4 import BeautifulSoup
r = requests.get("https://adminlte.io/themes/AdminLTE/pages/examples/profile.html")
soup = BeautifulSoup(r.text, 'html.parser')
name_box = soup.findAll('span', attrs={'class': 'username'})
for name in name_box:
print(name.find('a', href=True))