使用urllib和BeautifulSoup怕取维基百科的数据

来源:互联网 发布:smt贴片机编程 编辑:程序博客网 时间:2024/06/15 12:10

urllib是python3常用的一个网络链接库
BeautifulSoup是一个对文档操作的第三方库

from urllib.request import urlopenfrom bs4 import BeautifulSoupimport re#获取维基百科页面的数据resp = urlopen('https://en.wikipedia.org/wiki/Main_Page').read().decode('utf-8')#使用BeautifulSoup取解析维基百科页面soup = BeautifulSoup(resp,'html.parser')#使用正则表达式找出所有wiki字条urlList = soup.findAll('a',href=re.compile('^/wiki/'))for url in urlList:    if not re.search('\.(jpg|JPG)$',url['href']):        print(url.get_text(),'------','https://en.wikipedia.org' + url['href'])    pass
0 0
原创粉丝点击