猫眼电影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

最新文章

  1. 怎么在js中,访问viewbag,viewdata等等的值
  2. 【Java每日一题】20161102
  3. 深入理解:JavaScript原型与继承
  4. 【POJ3237】Tree 树链剖分+线段树
  5. python - socket - server
  6. 手机触摸touch事件
  7. C++之路进阶——codevs1204(寻找子串位置)
  8. SQLite的介绍 操作Sqlite 具体实例
  9. 一个简单的MDI示范程序(Delphi)
  10. MATLAB中的微积分运算(数值&amp;符号)
  11. JavaScript笔记1———js的数据类型
  12. Oracle单机Rman笔记[3]---RMAN脱机备份及命令基础介绍
  13. 画PCB
  14. mongodb的认证(authentication)与授权(authorization)
  15. linux下编译upx 3.93
  16. mysql 通过binlog 查看执行日志
  17. Redis集群学习笔记
  18. 【转】如何使用BehaviorSDK
  19. The Master of Science degree in Computer Scienc
  20. LeetCode: Word Ladder II 解题报告

热门文章

  1. Vue 组件之 Router
  2. JPQL
  3. cesium编程入门(三)开始使用cesium开发
  4. 从零开始学习前端开发 — 17、CSS3背景与渐变
  5. 【学习笔记】js下拉刷新、上拉加载 mescroll框架的使用
  6. C语言第二次实验报告
  7. 机器学习——kNN(2)示例:改进约会网站的配对效果
  8. Visio绘制用例图问题集锦
  9. django下命令行工具
  10. html动态生成的代码,绑定事件