如何从 <ul>,<li>l 列表标签获取数据以抓取数据

how to get data from <ul>,<li>l ist tags to scrape data

我已经从网页中提取数据,但我无法从没有唯一标识符的网页中提取数据

我已经尝试从具有唯一标识符的网页中提取数据,例如 class ,span ,id 但是当页面没有唯一标识符时该怎么办

url="https://dblp.org/"
r=requests.get(url)
print(r.content)
b=BeautifulSoup(r.text,"html.parser")
print(b.prettify())
a=b.find_all('ul',{"id":"browsable"})  #no id is available

它实际上显示 None 预期结果应该是可用链接列表

您可以对 li 元素中的 a 标签使用 type 选择器。以 body 父标签为例,您可以通过以下方式获取 li 子元素 a href

import requests
from bs4 import BeautifulSoup

url = 'https://dblp.org/'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'lxml')
links = [item['href'] for item in soup.select('body li a')]
print(links)

如果必须有父 ul 标签则:

body ul li a

值得注意的是,其中两个脚本标签还包含一个 json 结构,其中的链接可根据您的需要使用。