利用多进程获取猫眼电影top100
2024-10-19 23:51:53
猫眼电影top100 是数据是在加载网页时直接就已经加载了的,所以可以通过requests.get()方法去获取这个url的数据,能过对得到的数据进行分析从而获得top100的数据,
把获取的数据存入本地文件中.
代码如下:
import requests
import re
import json
from multiprocessing import Pool
#引用异常处理机制,这个类是所有异常类的超类,所以能全捕捉
from requests.exceptions import RequestException
def get_one_page(url):
try:
response=requests.get(url)
if response.status_code==200:
return response.text
return None
except RequestException :
return None 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>',re.S)
items=re.findall(pattern,html)
for item in items:
yield {
"index":item[0],
"iamge":item[1],
"title":item[2],
"actor":item[3].strip()[3:],
"time":item[4].strip()[5:],
"score":item[5]+item[6]
}
def write_to_file(content):
with open("result.txt","a",encoding="utf8") as f:
f.write(json.dumps(content,ensure_ascii=False)+"\n")
# f.close() 可以不关 def main(offset):
url="http://maoyan.com/board/4?offset="+str(offset)
html=get_one_page(url)
# print(html)
# parse_one_page(html)
for item in parse_one_page(html):
print(item)
write_to_file(item) # print(html) if __name__=="__main__":
# for i in range(10):
# main(i*10)
# 使用多进程
pool=Pool()
pool.map(main,[i*10 for i in range(10)])
猫眼电影top100每个页面显示10部电影,用offset 来构造网页,
结果如下:
github https://github.com/573320328/tools.git
最新文章
- 怎么在js中,访问viewbag,viewdata等等的值
- 【Java每日一题】20161102
- 深入理解:JavaScript原型与继承
- 【POJ3237】Tree 树链剖分+线段树
- python - socket - server
- 手机触摸touch事件
- C++之路进阶——codevs1204(寻找子串位置)
- SQLite的介绍 操作Sqlite 具体实例
- 一个简单的MDI示范程序(Delphi)
- MATLAB中的微积分运算(数值&;符号)
- JavaScript笔记1———js的数据类型
- Oracle单机Rman笔记[3]---RMAN脱机备份及命令基础介绍
- 画PCB
- mongodb的认证(authentication)与授权(authorization)
- linux下编译upx 3.93
- mysql 通过binlog 查看执行日志
- Redis集群学习笔记
- 【转】如何使用BehaviorSDK
- The Master of Science degree in Computer Scienc
- LeetCode: Word Ladder II 解题报告