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练习的更多相关文章

  1. Requests+BeautifulSoup+正则表达式爬取猫眼电影Top100(名称,演员,评分,封面,上映时间,简介)

    # encoding:utf-8 from requests.exceptions import RequestException import requests import re import j ...

  2. python3.6 利用requests和正则表达式爬取猫眼电影TOP100

    import requests from requests.exceptions import RequestException from multiprocessing import Pool im ...

  3. PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)

    利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...

  4. 爬虫练习之正则表达式爬取猫眼电影Top100

    #猫眼电影Top100import requests,re,timedef get_one_page(url): headers={ 'User-Agent':'Mozilla/5.0 (Window ...

  5. Requests+正则表达式爬取猫眼电影(TOP100榜)

    猫眼电影网址:www.maoyan.com 前言:网上一些大神已经对猫眼电影进行过爬取,所用的方法也是各有其优,最终目的是把影片排名.图片.名称.主要演员.上映时间与评分提取出来并保存到文件或者数据库 ...

  6. Python爬虫实战之Requests+正则表达式爬取猫眼电影Top100

    import requests from requests.exceptions import RequestException import re import json # from multip ...

  7. python爬虫从入门到放弃(九)之 Requests+正则表达式爬取猫眼电影TOP100

    import requests from requests.exceptions import RequestException import re import json from multipro ...

  8. 整理requests和正则表达式爬取猫眼Top100中遇到的问题及解决方案

    最近看崔庆才老师的爬虫课程,第一个实战课程是requests和正则表达式爬取猫眼电影Top100榜单.虽然理解崔老师每一步代码的实现过程,但自己敲代码的时候还是遇到了不少问题: 问题1:获取respo ...

  9. 14-Requests+正则表达式爬取猫眼电影

    '''Requests+正则表达式爬取猫眼电影TOP100''''''流程框架:抓去单页内容:利用requests请求目标站点,得到单个网页HTML代码,返回结果.正则表达式分析:根据HTML代码分析 ...

随机推荐

  1. luogu_P3373 solution

    luogu_P3373 solution Problme Description  Now, you have a known series, there are three operations: ...

  2. Excel—TIME函数简介与用法

    问题场景 算员工饱和度时,需要从实际考勤打卡时间中减去午休时间1.5个小时: 导出的时间时分秒是分开的,连接时分秒. 场景一 计算员工饱和度,需要减去午休时间,用下班打卡时间减去午休的1.5小时算出的 ...

  3. 详细了解JS Map,它和传统对象有什么区别?

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者.原文出处:https://www.codeproject.com/Articles/5278387/Under ...

  4. googleEarth

    中国航天科技集团有限公司 http://www.spacechina.com/n25/n144/n210/index.html Celestia [官网]: https://celestia.spac ...

  5. Fitness - 05.19

    倒计时226天 运动45分钟,共计9组,4.7公里.拉伸10分钟. 每组跑步3分钟(6.5KM/h),走路2分钟(5.5KM/h). 上周的跑步计划中断了,本周重复第三阶段的跑步计划. 一共掉了10斤 ...

  6. Android Studio从Eclipse导项目

    要是你只下了Android Studio 就不能用Eclipse导出gradle项目了 可以直接使用Android Studio导入模块,在Android Studio里Project算Eclipse ...

  7. 在centos6.5下搭建lnmp

    1.查看环境: [root@localhost /]# cat /etc/redhat-release CentOS release 6.5 (Final)   2.关掉防火墙(需要到国外地址下载软件 ...

  8. Oracle 回滚段undo

    Undo的作用 数据的回滚 一致性读 表的闪回(事务,查询的闪回..) 失败会话的恢复 回滚rollback操作 SQL> archive log list; ORA-01031: 权限不足 S ...

  9. Java 多线程实现多窗口同时售票简单功能

    package day162020072701.day1603; import java.util.concurrent.locks.Lock; import java.util.concurrent ...

  10. MVC中将枚举类型数据应用到下拉列表中的方法

    例如: public enum ItemTypes   {      Movie = 1,      Game = 2,      Book = 3   }    在MVC2.0中如何将以上枚举类型使 ...