首页 > 解决方案 > 尝试使用 BeautifulSoup 从 Wikipedia 中抓取参考链接,但我一直没有得到任何输出

问题描述

我正在尝试从 wiki 页面上抓取参考链接,但我一直得到 None 作为输出。

from bs4 import BeautifulSoup
import requests,lxml

webs=requests.get('https://en.wikipedia.org/wiki/Integrated_development_environment')
soup=BeautifulSoup(webs.content,'lxml')
links=soup.find('div', {'class':'reflist'})
print(links.get('href'))

标签: pythonbeautifulsoup

解决方案


我用给定的维基百科页面测试了这段代码,它返回一个包含每个链接作为字符串的列表。

from bs4 import BeautifulSoup
import requests,lxml

webs=requests.get('https://en.wikipedia.org/wiki/Integrated_development_environment')
soup=BeautifulSoup(webs.content,'html.parser')
links=soup.find_all('a', class_ = 'external text', rel = 'nofollow')
map = map(str , links)
result = []
for link in map:
    result.append(link[link.find('href=\"')+6:link.find('rel=')-2])
print(result)

推荐阅读