gpt4 book ai didi

python - BeautifulSoup PYTHON - 内部标签

转载 作者:太空宇宙 更新时间:2023-11-03 14:21:01 24 4
gpt4 key购买 nike

BeautifulSoup 的小问题:

from bs4 import BeautifulSoup
import requests

link = "http://www.cnnvd.org.cn/web/vulnerability/querylist.tag"

req = requests.get(link)
web = req.text
soup = BeautifulSoup(web, "lxml")

cve_name = []
cve_link = []

for par_ in soup.find_all('div', attrs={'class':'fl'}):
for link_ in par_.find_all('p'):
for text_ in link_.find_all('a'):
print (text_.string)
print (text_['href'])
print ("==========")
#cve_name.append(text_.string)
#cve_link.append(text_['href'])

它给了我两次记录:V 这可能很容易解决:V

最佳答案

相同的元素位于页面上的两个位置,因此您必须使用 find()/find_all() 来仅选择一个位置,即 find(class_ ='list_list')

soup.find(class_='list_list').find_all('div', attrs={'class':'fl'}):

完整代码:

from bs4 import BeautifulSoup
import requests

link = "http://www.cnnvd.org.cn/web/vulnerability/querylist.tag"

req = requests.get(link)
web = req.text
soup = BeautifulSoup(web, "lxml")

cve_name = []
cve_link = []


for par_ in soup.find(class_='list_list').find_all('div', attrs={'class':'fl'}):
print(len(par_))
for link_ in par_.find_all('p'):
for text_ in link_.find_all('a'):
print (text_.string)
print (text_['href'])
print ("==========")
#cve_name.append(text_.string)
#cve_link.append(text_['href'])

关于python - BeautifulSoup PYTHON - 内部标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47939702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com