1.豆瓣top250
from bs4 import BeautifulSoup # 网页解析,获取数据 import re # 正则表达,实现文字匹配 import urllib.request, urllib.error # 制定url,获取网页数据 import xlwt # 进行excel操作 import sqlite3 # 进行SQLite数据库操作 def main(): # 1.爬取网页 baseurl = "https://movie.douban.com/top250?start=" datalist = getData(baseurl) savepath = "豆瓣电影Top250.xlsx" # 3.保存数据 saveData(datalist, savepath) # 影片链接的规则 findLink = re.compile(r'<a href="(.*?)">') # 创建正则表达式对象,表示规则(字符串表达式) # 影片的图片的规则 findImgSrc = re.compile(r'<img.*src="(.*?)"', re.S) # re.S让换行符包含在字符中 # 影片片名 findTitle = re.compile(r'<span class="title">(.*)</span>') # 影片评分 findRating = re.compile(r'<span class="rating_num" property="v:average">(.*)</span>') # 找到评价人数 findJudge = re.compile(r'<span>(\d*)人评价</span>') # 找到概况 findInq = re.compile(r'<span class="inq">(.*)</span>') # 找到影片相关内容 findBd = re.compile(r'<p class="">(.*?)</p>', re.S) def getData(baseurl): datalist = [] for i in range(0, 10): # 调用获取页面信息的函数,10次 url = baseurl + str(i * 25) html = askURL(url) # 保存获取到的网页源码 # 2.逐一解析数据 soup = BeautifulSoup(html, "html.parser") for item in soup.find_all('div', class_="item"): # 查找符合要求的字符串,形成一个链表 # print(item) #测试 data = [] # 保存一部电影的所有信息 item = str(item) link = re.findall(findLink, item)[0] # re库用来通过正则表达式查找指定内容 data.append(link) imgSrc = re.findall(findImgSrc, item)[0] # 添加图片 data.append(imgSrc) titles = re.findall(findTitle, item) # 片名可能只有一个中文名 if (len(titles) == 2): ctitle = titles[0] data.append(ctitle) # 添加中文名 otitle = titles[1].replace("/", "") # 去掉无关的符号 data.append(otitle) # 添加外国名 else: data.append(titles[0]) data.append(' ') # 外国名留空 rating = re.findall(findRating, item)[0] # 添加评分 data.append(rating) judgeNom = re.findall(findJudge, item)[0] # 添加评价人数 data.append(judgeNom) inq = re.findall(findInq, item) if len(inq) != 0: inq = inq[0].replace("。", "") # 去掉句号 data.append(inq) # 添加概述 else: data.append(" ") # 留空 bd = re.findall(findBd, item)[0] bd = re.sub('<br(\s+)?>(\s+)?', " ", bd) # 去掉<br> bd = re.sub('/', " ", bd) # 替换/ data.append(bd.strip()) # 去掉前后空格 datalist.append(data) return datalist def saveData(datalist, savepath): print("save....") book = xlwt.Workbook(encoding="utf-8", style_compression=0) # 创建workbook对象 sheet = book.add_sheet('豆瓣电影Top250', cell_overwrite_ok=True) # 创建工作表 col = ("电影详情链接", "图片链接", "影片中文名", "影片外国名", "评分", "评价数", "概况", "相关信息") for i in range(0, 8): sheet.write(0, i, col[i]) for i in range(0, 250): print("第%d条" % i) data = datalist[i] for j in range(0, 8): sheet.write(i + 1, j, data[j]) book.save(savepath) def askURL(url): head = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"} request = urllib.request.Request(url, headers=head) html = "" try: response = urllib.request.urlopen(request) html = response.read().decode("utf-8") except urllib.error.URLError as e: if hasattr(e, "code"): print(e.code) if hasattr(e, "reason"): print(e.reason) return html if __name__ == '__main__': main() print("爬取完毕")
2.酷6视频
import json import requests def ku6_spider(): for page in range(0,11): print('正在抓取第{}页数据'.format(page+1)) #1.确定URL路径,headers参数 base_url = 'https://www.ku6.com/video/feed' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36' } params = { 'pageNo': str(page), 'pageSize': '40', 'subjectId': '76', } #2.模拟浏览器-requests发送请求获取数据 response = requests.get(base_url,headers=headers,params=params) res_data = response.text #print(res_data) #3.解析数据 json_data = json.loads(res_data) #--字典 data_list = json_data['data'] #遍历列表 for data in data_list: #print(data) video_name = data['title']+".mp4" video_url = data['playUrl'] #print(video_url,video_name) print('下载中:',video_name) video_data = requests.get(video_url,headers=headers).content #4.保存数据 with open('video\\'+video_name,'wb') as f: f.write(video_data) print('下载完成...') if __name__ == '__main__': ku6_spider()
邮件:2965191336@qq.com
文章多处存在借鉴,如有侵权请联系修改删除