多种方法爬取猫眼电影Top100排行榜,保存到csv文件,下载封面图
参考链接:
https://blog.csdn.net/BF02jgtRS00XKtCx/article/details/83663400
https://www.makcyun.top/web_scraping_withpython1.html
因猫眼网站有些更新,参考链接中的部分代码执行报错,特修改一下
#!/usr/bin/env python
# -*- coding: utf-8 -*- import csv
import re
from multiprocessing.pool import Pool import requests
from bs4 import BeautifulSoup
from lxml import etree
from requests.exceptions import RequestException def get_one_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
else:
return None
except RequestException:
return None # 获取封面大图
def get_thumb(url):
# url = 'https://p0.meituan.net/movie/46c29a8b8d8424bdda7715e6fd779c66235684.jpg@160w_220h_1e_1c'
pattern = re.compile(r'(.*?)@.*?')
thumb = re.search(pattern, url)
return thumb.group(1)
# http://p0.meituan.net/movie/5420be40e3b755ffe04779b9b199e935256906.jpg@160w_220h_1e_1c
# 去掉@160w_220h_1e_1c就是大图 # 提取上映时间函数
def get_release_time(data):
pattern = re.compile(r'(.*?)(\(|$)')
items = re.search(pattern, data)
if items is None:
return '未知'
return items.group(1) # 返回匹配到的第一个括号(.*?)中结果即时间 # 提取国家/地区函数
def get_release_area(data):
pattern = re.compile(r'.*\((.*)\)')
# $表示匹配一行字符串的结尾,这里就是(.*?);(|$,表示匹配字符串含有(,或者只有(.*?)
items = re.search(pattern, data)
if items is None:
return '未知'
return items.group(1) # 使用正则表达式的写法
def parse_one_page(html):
pattern = re.compile(
'<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',
re.S) # re.S表示匹配任意字符,如果不加,则无法匹配换行符
items = re.findall(pattern, html)
for item in items:
yield {
'index': item[0],
'thumb': get_thumb(item[1]), # 定义get_thumb()方法进一步处理网址
'name': item[2],
'star': item[3].strip()[3:],
# 'time': item[4].strip()[5:],
# 用一个方法分别提取time里的日期和地区
'time': get_release_time(item[4].strip()[5:]),
'area': get_release_area(item[4].strip()[5:]),
'score': item[5].strip() + item[6].strip()
# 评分score由整数+小数两部分组成
} # lxml结合xpath提取
def parse_one_page2(html):
parse = etree.HTML(html)
items = parse.xpath('/html/body/div[4]//div//dd')
for item in items:
yield {
'index': item.xpath('./i/text()')[0],
'thumb': get_thumb(str(item.xpath('./a/img[2]/@data-src')[0].strip())),
'name': item.xpath('./div/div/div[1]/p[1]/a/@title')[0],
'star': item.xpath('.//p[@class="star"]/text()')[0].strip()[3:],
'realease_time': get_release_time(item.xpath('.//p[@class="releasetime"]/text()')[0].strip()[5:]),
'area': get_release_area(item.xpath('.//p[@class="releasetime"]/text()')[0].strip()[5:]),
'score': item.xpath('./div/div/div[2]/p/i[1]/text()')[0] + item.xpath('./div/div/div[2]/p/i[2]/text()')[0],
} # 使用BeautifulSoup结合css选择器
def parse_one_page3(html):
soup = BeautifulSoup(html, 'lxml')
items = range(10)
for item in items:
yield {
'index': soup.select('i.board-index')[item].string,
'thumb': get_thumb(soup.select('.board-img')[item]['data-src']),
'name': soup.select('.name a')[item].string,
'star': soup.select('.star')[item].string.strip()[3:],
'time': get_release_time(soup.select('.releasetime')[item].string.strip()[5:]),
'area': get_release_area(soup.select('.releasetime')[item].string.strip()[5:]),
'score': soup.select('.integer')[item].string + soup.select('.fraction')[item].string
} # Beautiful Soup + find_all函数提取
def parse_one_page4(html):
soup = BeautifulSoup(html, 'lxml')
items = range(10)
for item in items:
yield {
'index': soup.find_all(class_='board-index')[item].string,
'thumb': get_thumb(soup.find_all(class_='board-img')[item].attrs['data-src']),
'name': soup.find_all(name='p', attrs={'class': 'name'})[item].string,
'star': soup.find_all(name='p', attrs={'class': 'star'})[item].string.strip()[3:],
'time': get_release_time(soup.find_all(class_='releasetime')[item].string.strip()[5:]),
'area': get_release_area(soup.find_all(class_='releasetime')[item].string.strip()[5:]),
'score': soup.find_all(name='i', attrs={'class': 'integer'})[item].string +
soup.find_all(name='i', attrs={'class': 'fraction'})[item].string
} # 数据存储到csv
def write_to_file3(item):
with open('猫眼top100.csv', 'a', encoding='utf_8_sig', newline='') as f:
# 'a'为追加模式(添加)
# utf_8_sig格式导出csv不乱码
fieldnames = ['index', 'thumb', 'name', 'star', 'time', 'area', 'score']
w = csv.DictWriter(f, fieldnames=fieldnames)
# w.writeheader()
w.writerow(item) # 下载封面图片
def download_thumb(name, url, num):
try:
response = requests.get(url)
with open('封面图/' + name + '.jpg', 'wb') as f:
f.write(response.content)
print('第%s部电影封面下载完毕' % num)
print('------')
except RequestException as e:
print(e)
pass
# 不能是w,否则会报错,因为图片是二进制数据所以要用wb def main(offset):
url = 'http://maoyan.com/board/4?offset=' + str(offset)
html = get_one_page(url)
for item in parse_one_page4(html):
write_to_file3(item)
download_thumb(item['name'], item['thumb'], item['index']) if __name__ == '__main__':
pool = Pool()
pool.map(main, [i * 10 for i in range(10)])
多种方法爬取猫眼电影Top100排行榜,保存到csv文件,下载封面图的更多相关文章
- 多线程爬取猫眼电影TOP100并保存到mongo数据库中
import requests import re import json from requests.exceptions import RequestException from multipro ...
- python爬虫入门新手向实战 - 爬取猫眼电影Top100排行榜
本次主要爬取Top100电影榜单的电影名.主演和上映时间, 同时保存为excel表个形式, 其他相似榜单也都可以依葫芦画瓢 首先打开要爬取的网址https://maoyan.com/board/4, ...
- 50 行代码教你爬取猫眼电影 TOP100 榜所有信息
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天,恋习Python的手把手系列,手把手教你入门Python爬虫,爬取猫眼电影TOP100榜信息,将涉及到基础爬虫 ...
- 40行代码爬取猫眼电影TOP100榜所有信息
主要内容: 一.基础爬虫框架的三大模块 二.完整代码解析及效果展示 1️⃣ 基础爬虫框架的三大模块 1.HTML下载器:利用requests模块下载HTML网页. 2.HTML解析器:利用re正则表 ...
- 用requests库爬取猫眼电影Top100
这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- # [爬虫Demo] pyquery+csv爬取猫眼电影top100
目录 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 代码君 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 https://maoyan.co ...
- 爬虫系列(1)-----python爬取猫眼电影top100榜
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...
- python 爬取猫眼电影top100数据
最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel. 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据 ...
随机推荐
- iOS - 社会化分享-微信分享,朋友圈分享
我仅仅做了文字和图片分享功能 1. TARGETS - Info - URL Types identifier -> weixin URL Schemes -> 应用id 2.在AppD ...
- Xcode 自己主动生成版本技术最佳实践
在 bloglovin ,我们使用自己主动生成版本来设置Xcode,使当前的版本为在Git活跃的分支上 的提交数. 它一直正常工作着.但我们的技术也不是一帆风顺的. 糟糕的老方法 我们使用的技术是来自 ...
- 君正Ingenic X1000E_halley2 更改Logo
有两种方法可以改变开机logo,编译进内核或者修改u-boot. <一>.编译进内核 一. 制作LOGO图片(可以使用gimp) 1. 制作一个.ppm格式图片(logo_tvu_clut ...
- Codeforces--630J--Divisibility(公倍数)
J - Divisibility Crawling in process... Crawling failed Time Limit:500MS Memory Limit:65536KB ...
- 杂项-Company:ShineYoo
ylbtech-杂项-Company:ShineYoo 1. 网站返回顶部 1. 2. 3. 4. 2. 网站测试返回顶部 1. 2. 3.家服宝返回顶部 0.首页 http://www.jiafb. ...
- android apk 防止反编译技术第二篇-运行时修改Dalvik指令
上一篇我们讲了apk防止反编译技术中的加壳技术,如果有不明白的可以查看我的上一篇博客http://my.oschina.net/u/2323218/blog/393372.接下来我们将介绍另一种防止a ...
- Django day15 (二) csrf的 跨站请求伪造 与 局部禁用 , 局部使用
一: csrf 的跨站请求伪造 二: csrf 的局部禁用 , 局部使用
- C指针基础知识
指针的声明 C语言声明格式:"类型 变量名;" 基本类型:int hoge; 指针类型:int *pointer; 区别在于: 声明 含义 int hoge; 声明整数类型的变量 ...
- go-swagger的简单使用
一.下载go-swagger go-swagger 官方下载 根据不同个的操作系统选择对应的 二.添加环境变量 2.1 window swagger_windows_amd64.exe 将swagge ...
- ccf 201803-4 棋局评估 (对抗搜索)
棋局评估 问题描述 Alice和Bob正在玩井字棋游戏. 井字棋游戏的规则很简单:两人轮流往3*3的棋盘中放棋子,Alice放的是“X”,Bob放的是“O”,Alice执先.当同一种棋子占据一行.一列 ...