【原创】python爬虫- 获取bilibili封面

admin 2023年1月8日02:29:43评论25 views字数 1506阅读5分1秒阅读模式
# !/usr/bin/python
# -*- coding:utf-8 -*-
#time: 2020.3.7
import requests,re,os,time
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0'}
def chart():
    print('\n请输入要提取图片的网址:')
    url = input()
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'lxml')#用BeautifulSoup+lxml解析器解析内容
        for position in soup.find_all(attrs={'property': 'og:image'}):#切出并遍历property:og:image所有结果
            position_str = str(position)#结果转化成str形式
            position_list = re.findall('<meta.*?content="(.*?)".*?>', position_str)#用正则匹配出无用的留下地址
            position_str = str(position_list[0])#再将list形式转换成str形式并最终切出图片地址
            position_rq = requests.get(position_str)  #最后将str转换成requests.models.Response形式
            name = os.path.split(position_str)[1] #切出文件名
            if os.path.exists('c:/img') == True:
                print('\n正在写入文件\t>>>>>>>>>>>>>>>>>>>>>')
                with open('c:/img/%s' + name, 'wb') as f:  # 以字符串的形式写入
                    f.write(position_rq.content)  # 只接受str形式
            else:
                print('\n在C盘创建img的文件夹')
                os.mkdir('c:/img')
                print('\n正在写入文件\t>>>>>>>>>>>>>>>>>>>>>')
                with open('c:/img/%s' + name, 'wb') as f:  # 以字符串的形式写入
                    f.write(position_rq.content)  # 只接受str形式
            print('\n文件已保存\tC:/img')
chart()
number = 1
while number > 0:
    print('\n是否继续获取\t(y/n):')
    information = input()
    if information == 'y':
        chart()
    elif information == 'n':
        print('\n记得下次再来哦QAQ\t(3s后将自动关闭窗口...)')
        time.sleep(3)
        break
    else:
        print('\n格式错误,请重新输入:')

最后可以打包成exe文件

详情见12分钟后

FROM:浅浅淡淡[hellohy]

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年1月8日02:29:43
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【原创】python爬虫- 获取bilibili封面http://cn-sec.com/archives/1443473.html

发表评论

匿名网友 填写信息