requests和正则表达式爬取猫眼电影Top100练习
1 import requests
2 import re
3 from multiprocessing import Pool
4 from requests.exceptions import RequestException
5 import json
6 import time
7
8
9 # 抓取单页内容
10 def get_one_page(url):
11 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
12 "Chrome/85.0.4183.121 Safari/537.36"}
13 try:
14 response = requests.get(url, headers=headers)
15 if response.status_code == 200:
16 return response.text
17 else:
18 return None
19 except RequestException:
20 return None
21
22
23 # 解析单页内容
24 def parser_one_page(html):
25 pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a.*?>(.*?)</a>'
26 + '.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>'
27 + '.*?</dd>', re.S)
28 contents = re.findall(pattern, html)
29 for content in contents:
30 yield { # 生成一个generator,对区域内的内容进行迭代处理
31 'index': content[0],
32 'image': content[1],
33 'name': content[2].strip(),
34 'actor': content[3].strip()[3:],
35 'time': content[4][5:],
36 'score': content[5]+content[6]
37 }
38
39
40 # 将单页内容写入文件
41 def write_to_file(content):
42 with open('猫眼电影.txt', 'a', encoding='utf-8') as f:
43 f.write(json.dumps(content, ensure_ascii=False) + '\n')
44 f.close()
45
46
47 def main(offset):
48 url = 'http://maoyan.com/board/4?offset=' + str(offset)
49 html = get_one_page(url)
50 for item in parser_one_page(html):
51 write_to_file(item)
52
53 if __name__ == "__main__":
54 time1 = time.time()
55 for i in range(0, 100, 10):
56 main(i)
57 time2 = time.time()
58 pool = Pool() # 使用多进程提高爬取效率
59 pool.map(main, [i*10 for i in range(0, 10)])
60 time3 = time.time()
61 print(time2-time1) # for...in花费时间
62 print(time3-time2) # 多线程花费时间
运行时间如下:

补充对yield用法的理解:
相关博客文章:https://blog.csdn.net/qq_33472765/article/details/80839417
requests和正则表达式爬取猫眼电影Top100练习的更多相关文章
- Requests+BeautifulSoup+正则表达式爬取猫眼电影Top100(名称,演员,评分,封面,上映时间,简介)
# encoding:utf-8 from requests.exceptions import RequestException import requests import re import j ...
- python3.6 利用requests和正则表达式爬取猫眼电影TOP100
import requests from requests.exceptions import RequestException from multiprocessing import Pool im ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- 爬虫练习之正则表达式爬取猫眼电影Top100
#猫眼电影Top100import requests,re,timedef get_one_page(url): headers={ 'User-Agent':'Mozilla/5.0 (Window ...
- Requests+正则表达式爬取猫眼电影(TOP100榜)
猫眼电影网址:www.maoyan.com 前言:网上一些大神已经对猫眼电影进行过爬取,所用的方法也是各有其优,最终目的是把影片排名.图片.名称.主要演员.上映时间与评分提取出来并保存到文件或者数据库 ...
- Python爬虫实战之Requests+正则表达式爬取猫眼电影Top100
import requests from requests.exceptions import RequestException import re import json # from multip ...
- python爬虫从入门到放弃(九)之 Requests+正则表达式爬取猫眼电影TOP100
import requests from requests.exceptions import RequestException import re import json from multipro ...
- 整理requests和正则表达式爬取猫眼Top100中遇到的问题及解决方案
最近看崔庆才老师的爬虫课程,第一个实战课程是requests和正则表达式爬取猫眼电影Top100榜单.虽然理解崔老师每一步代码的实现过程,但自己敲代码的时候还是遇到了不少问题: 问题1:获取respo ...
- 14-Requests+正则表达式爬取猫眼电影
'''Requests+正则表达式爬取猫眼电影TOP100''''''流程框架:抓去单页内容:利用requests请求目标站点,得到单个网页HTML代码,返回结果.正则表达式分析:根据HTML代码分析 ...
随机推荐
- JDK16关于TCP和UDP的优化
文章转自belaban.blogspot.com Double your performance: virtual threads (fibers) and JDK 15/16!If you use ...
- Maxscript中渲染中文版Vray完成贴图(VrayCompleteMap)的方法
Objbakeproperties = $.INodeBakeProperties; --选定对象的烘培节点 Prjbakeproperties = $.INodeBakeProjProperties ...
- 开源基于lua gc管理c++对象的cocos2dx lua绑定方案
cocos2dx目前lua对应的c++对象的生命周期管理,是基于c++析构函数的,也就是生命周期可能存在不一致,比如c++对象已经释放,而lua对象还存在,如果这时候再使用,会有宕机的风险,为此我开发 ...
- 关于List的remove()方法
最近遇到一个小问题,我将其简化为下列代码,List的remove()方法在下列颜色注重的代码执行的源码也是不同的~ List<Integer> list=new ArrayList< ...
- Codeforces1409 题解(A-F)
A. Yet Another Two Integers Problem 最优的操作中,\(k = \min(10, abs(a - b))\),记\(d=abs(a-b)\),最终的答案为\(ans ...
- 2020重新出发,NOSQL,Redis主从复制
Redis主从复制 尽管 Redis 的性能很好,但是有时候依旧满足不了应用的需要,比如过多的用户进入主页,导致 Redis 被频繁访问,此时就存在大量的读操作. 对于一些热门网站的某个时刻(比如促销 ...
- 07_Python语法示例(基础语法,文件操作,异常处理)
1.写程序在终端输出图形 ######## # # # # ######## print("#" * 8) print("#" + " " ...
- Python 零基础快速入门!
“人生苦短,我学python”是编程届的名言.用python写小脚本的便捷性,让很多其他语言的学习者把python当作辅助语言.拥有了某一个语言的功底,再来学习另外一种语言应该是十分快速的.编程理念都 ...
- mysql InnoDB引擎是否支持hash索引
看一下mysql官方文档:https://dev.mysql.com/doc/refman/5.7/en/create-index.html , 从上面的图中可以得知,mysql 是支持hash索引的 ...
- Azure技术系列之Redis篇---第一章数据缓存
嘈杂和忙碌的生活占据占据了生活的每一天,好久没有静下心来对自己喜欢的技术进行归纳总结了.痛定思痛,今天开始开荒,把之前研究的技术进行归纳总结,先从Azure的Redis的开发技术开始. Azure 的 ...