#?encode:utf-8
#?引入開發(fā)包
from?urllib?import?request
from?bs4?import?BeautifulSoup
import?re
#?攜帶User-Agent的頭,模擬真實瀏覽器來請求URL,并把結(jié)果用?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標(biāo)簽的href屬性
listUrls?=?soup.findAll('a',?href=re.compile('^//www\.bilibili\.com/video/'))
#?輸出所有的詞條對應(yīng)的名稱和url
for?url?in?listUrls:
????print(url.get_text(),?'<---->',?url['href'])
2019-08-20
url修改成:https://api.bilibili.com/x/web-interface/search/type?search_type=video&highlight=1&keyword=%E5%BE%90%E5%A4%A7SAO&page=3&jsonp=jsonp&callback=__jp1直接設(shè)置page就可以了