马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Python 爬取B站视频链接&标题
先放源码:
from requests import get
from lxml import etree
def open_url(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'
}
res = get(url, headers = headers)
return res
def get_Video(res):
html = etree.HTML(res.text)
name = html.xpath('//*[@class="video-item matrix"]/a/@title')
href = html.xpath('//*[@class="video-item matrix"]/a/@href')
for i in range(len(href)):
href[i]='https:' + href[i]
return name, href
def main():
keyw = input("请输入关键字:")
url = 'https://search.bilibili.com/all?keyword=%s&from_source=nav_search&spm_id_from=333.851.b_696e7465726e6174696f6e616c486561646572.15' % keyw
res = open_url(url)
name, href = get_Video(res)
temp = {} # 这个temp用于改进,可以删掉
for i in range(len(name)):
print(name[i], ':', href[i])
temp[i+1] = name[i], href[i]
if __name__ == "__main__":
main()
用bs4可谓极其简单,但是我想练练XPath
感觉XPath好简单,把路径关系写进去就好了。 |