requests + 正则表达式 获取 ‘猫眼电影top100’。
使用 进程池Pool 提高爬取数据的速度。
1 # !/usr/bin/python
2 # -*- coding:utf-8 -*-
3 import requests
4 from requests.exceptions import RequestException
5 import re
6 import json
7 from multiprocessing import Pool,Lock
8
9
10 # 获取单页数据信息;
11 def get_one_page(url, headers):
12 try:
13 response = requests.get(url, headers=headers)
14 if response.status_code == 200:
15 return response.text
16 return None
17 except RequestException:
18 return None
19
20 # 使用正则表达式解析数据;
21 def parse_one_page(html):
22 pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a.*?>'
23 +'(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">'
24 +'(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S)
25 items = re.findall(pattern, html)
26 # 将获取的数据以字典形式返回;
27 for item in items:
28 yield {
29 'index': item[0],
30 'image': item[1],
31 'title': item[2],
32 'actor': item[3].strip()[3:],
33 'time': item[4].strip()[5:],
34 'score': item[5]+item[6]
35 }
36
37 # 将字典转换为字符串保存,
38 def write_to_file(content):
39 lock.acquire()
40 with open('result.txt', 'a', encoding="utf-8") as f:
41 f.write(json.dumps(content, ensure_ascii=False) + "\n")
42 f.close()
43 lock.release()
44
45 def init(l):
46 global lock
47 lock = l
48
49 # 传入爬取链接,运行函数;
50 def main(offset):
51 url = "https://maoyan.com/board/4?offset=" + str(offset)
52 headers = {
53 "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
54 }
55 html = get_one_page(url, headers=headers)
56 for item in parse_one_page(html):
57 write_to_file(item)
58
59 if __name__ == '__main__':
60 # for i in range(10):
61 # main(i*10)
62 # 使用 进程池 提高爬取速度;
63 lock = Lock()
64 Pool = Pool(initializer=init, initargs=(lock,))
65 Pool.map(main, [i*10 for i in range(10)])
python限定了多进程要调用的函数不能是类方法,需把多进程调用的函数放到类外面,或者变成静态函数。
但静态函数不能被该类的方法调用( self.ProcessWorker 形式),需在外部调用:如 mc = MyClass(), mc.ProcessWorker 或 MyClass().ProcessWorker 。
python 多进程编程中进程池锁共享问题参考博客:https://blog.csdn.net/qq_27292549/article/details/78929296
# 将数组中的每个元素提取出来当作函数的参数,创建一个个进程,放进进程池中;
# 第一个参数是函数,第二个参数是迭代器,将迭代器中的数字作为参数依次传入函数中
requests + 正则表达式 获取 ‘猫眼电影top100’。的更多相关文章
- requests+正则表达式提取猫眼电影top100
#requests+正则表达式提取猫眼电影top100 import requests import re import json from requests.exceptions import Re ...
- 1.requests+正则表达式爬猫眼电影TOP100
import requests from requests.exceptions import RequestException def get_one_page(url):try: response ...
- 使用requests爬取猫眼电影TOP100榜单
Requests是一个很方便的python网络编程库,用官方的话是"非转基因,可以安全食用".里面封装了很多的方法,避免了urllib/urllib2的繁琐. 这一节使用reque ...
- 利用多进程获取猫眼电影top100
猫眼电影top100 是数据是在加载网页时直接就已经加载了的,所以可以通过requests.get()方法去获取这个url的数据,能过对得到的数据进行分析从而获得top100的数据, 把获取的数据存入 ...
- Python爬虫之requests+正则表达式抓取猫眼电影top100以及瓜子二手网二手车信息(四)
requests+正则表达式抓取猫眼电影top100 一.首先我们先分析下网页结构 可以看到第一页的URL和第二页的URL的区别在于offset的值,第一页为0,第二页为10,以此类推. 二.< ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- 用requests库爬取猫眼电影Top100
这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...
- 爬虫练习之正则表达式爬取猫眼电影Top100
#猫眼电影Top100import requests,re,timedef get_one_page(url): headers={ 'User-Agent':'Mozilla/5.0 (Window ...
- Python爬虫项目--爬取猫眼电影Top100榜
本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构, ...
随机推荐
- 第12篇-认识CodeletMark
InterpreterCodelet依赖CodeletMark完成自动创建和初始化.CodeletMark继承自ResourceMark,允许自动析构,执行的主要操作就是,会按照Interpreter ...
- EZpop分析
首先源代码如下 <?php class Modifier { protected $var; public function append($value){ include($value); } ...
- C语言定义常量
/* #define 标识符 #define day main中day=10;仅一次赋值*/ 错误 #define 标识符 常量值 #define day 10;
- 2020年秋游戏开发-Gluttonous Snake
此作业要求参考https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11577 GitHub地址为https://github.com/15011 ...
- redis>lua脚本
String lua="local num=redis.call('incr',KEYS[1])\n"+"if tonumber(num)==1 then\n" ...
- 及上一篇linux安装mysql的说明
mysql8.0安全策略 1 密码规定:数字英文大小写加特殊符号组成(可以不按照规则,详情去百度设置) 2. mysql数据库用户密码字段不再是password 而是authentication_st ...
- 备忘:Linux内核编程的几个注意事项
虚拟地址转物理地址要用__pa 内核程序创建的一段地址连续的共享内存,通过内存映射可以让用户态进程存取.之前在RHEL/CentOS的x86_64架构上工作正常.后来在aarch64架构的银河麒麟(L ...
- MySQL高可用主从复制新增slave
原文转自:https://www.cnblogs.com/itzgr/p/10233932.html作者:木二 目录 一 基础环境 二 新增slave2方案 2.1 方案1:-复制主库 2.2 方案2 ...
- MySQL-存储引擎-1
一.MySQL存储引擎 mysql> create table country( -> country_id smallint unsigned not null auto_increme ...
- VSCode——滚动鼠标控制字体大小
第一步:找到设置 文件-->首选项-->设置 第二步:打开settings.json文件 第三步:在settings.json文件中添加 "editor.mouseWheelZo ...