利用urllib和BeautifulSoup爬取维基百科的词条

xiaoxiao2021-02-28  87

#引入开发包 from urllib.request import urlopen from bs4 import BeautifulSoup import re #请求URL并把结果用UTF-8编码 resp=urlopen("https://zh.wikipedia.org/wiki/Wikipedia:首页").read().decode("utf-8") #使用BeautifulSoup去解析 soup=BeautifulSoup(resp,"html.parser") #print(soup) #获取所有以/wiki/开头的a标签的href属性 listUrl=soup.findAll("a",href=re.compile("^/wiki/")) #输出所有词条对应的名称和URL for link in listUrl: if not re.search("\.(jpg|JPG)$",link["href"]): print(link.get_text(),"<----->","https://zh.wikipedia.org"+link["href"])

转载请注明原文地址: https://www.6miu.com/read-54408.html

最新回复(0)