# encode:utf-8
# 引入开发包
from urllib import request
from bs4 import BeautifulSoup
import re
# 携带User-Agent的头,模拟真实浏览器来请求URL,并把结果用 utf-8 编码
req = request.Request('https://search.bilibili.com/all?keyword=%E5%BE%90%E5%A4%A7SAO')
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36')
resq = request.urlopen(req)
# 使用 BeautifulSoup 去解析
soup = BeautifulSoup(resq, 'html.parser')
# 获取所有以//www.bilibili.com/video/开头的a标签的href属性
listUrls = soup.findAll('a', href=re.compile('^//www\.bilibili\.com/video/'))
# 输出所有的词条对应的名称和url
for url in listUrls:
print(url.get_text(), '<---->', url['href'])