#!/usr/bin/env python
# -*- coding: utf- -*-
# @Author: Dang Kai
# @Date: -- ::
# @Last Modified time: -- ::
# @E-mail: @qq.com
# @Description:增加多进程 # http://maoyan.com/board/4
# http://maoyan.com/board/4?offset=20 import requests
import re
import json
from multiprocessing import Pool
from requests.exceptions import RequestException def get_one_page(url, headers):
'''获取单页的html'''
try:
reponse = requests.get(url, headers=headers)
if reponse.status_code == :
return reponse.text
else:
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>.*?</dd>', re.S) items = re.findall(pattern, html)
# print(items)
for item in items:
yield{
'index': item[],
'image': item[],
'title': item[],
'actor': item[].strip()[:],
'starttime': item[].strip()[:],
'score': item[] + item[]
} def write_to_file(content):
'''写入文件'''
with open('result.txt', 'a', encoding='utf-8') as f:
f.write(json.dumps(content, ensure_ascii=False) + '\n')
f.close() def main(offset):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36'}
html = get_one_page(
'http://maoyan.com/board/4?offset=' + str(offset), headers)
# print(html)
for item in parse_one_page(html):
print(item)
write_to_file(item)
if __name__ == '__main__':
# for i in range():
# main(i*)
pool = Pool()
pool.map(main, {i* for i in range()})

python 爬取猫眼榜单100(二)--多个页面以及多进程的更多相关文章

  1. 关于爬虫的日常复习(8)—— 实战:request+正则爬取猫眼榜单top100

  2. Python 爬取 猫眼 top100 电影例子

    一个Python 爬取猫眼top100的小栗子 import json import requests import re from multiprocessing import Pool #//进程 ...

  3. 爬虫系列(1)-----python爬取猫眼电影top100榜

    对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...

  4. Python 爬取猫眼电影最受期待榜

     主要爬取猫眼电影最受期待榜的电影排名.图片链接.名称.主演.上映时间. 思路:1.定义一个获取网页源代码的函数: 2.定义一个解析网页源代码的函数: 3.定义一个将解析的数据保存为本地文件的函数: ...

  5. python 爬取猫眼电影top100数据

    最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel. 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据 ...

  6. 票房和口碑称霸国庆档,用 Python 爬取猫眼评论区看看电影《我和我的家乡》到底有多牛

    今年的国庆档电影市场的表现还是比较强势的,两名主力<我和我的家乡>和<姜子牙>起到了很好的带头作用. <姜子牙>首日破 2 亿,一举刷新由<哪吒之魔童降世&g ...

  7. Python爬取猫眼电影100榜并保存到excel表格

    首先我们前期要导入的第三方类库有; 通过猫眼电影100榜的源码可以看到很有规律 如: 亦或者是: 根据规律我们可以得到非贪婪的正则表达式 """<div class ...

  8. Python爬取猫眼电影《飞驰人生》47858万条评论并对其进行数据分析

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Yura不说数据说 ,PYuraL PS:如有需要Python学习资 ...

  9. python爬取猫眼电影top100

    最近想研究下python爬虫,于是就找了些练习项目试试手,熟悉一下,猫眼电影可能就是那种最简单的了. 1 看下猫眼电影的top100页面 分了10页,url为:https://maoyan.com/b ...

随机推荐

  1. 如何 安装Yii2的高级应用程序模板

    参考 https://blog.csdn.net/youngqj/article/details/46689051

  2. OCR 维护 crsd.log

    ###########sample 1 OCR corruption messages are reported in crsd.log, automatic OCR backup is failin ...

  3. ORA-1000的问题 Cursor 过多 (文档 ID 18591.1)

      #查看用户cursor的使用情况 col sid for a9999999999 col osuser for a20 col machine for a20 col num_curs for a ...

  4. 运行结果:Spring Bean的生命周期

    详见:http://www.cnblogs.com/zrtqsk/p/3735273.html https://blog.csdn.net/qq_23473123/article/details/76 ...

  5. [转]使用Node.js完成的第一个项目的实践总结

    本文转自:http://blog.csdn.net/yanghua_kobe/article/details/17199417 https://github.com/yanghua/FixedAsse ...

  6. (转)shell中各种括号的作用()、(())、[]、[[]]、{}

    shell中各种括号的作用().(()).[].[[]].{} 原文:http://www.jb51.net/article/60326.htm http://blog.csdn.net/good_h ...

  7. Nginx实践:(1) Nginx安装及日志配置

    1. 安装 (1)  Nginx下载地址:https://nginx.org/download/nginx-1.14.0.tar.gz (2)  安装时可能出现依赖库不存在,比如prec包,可以使用y ...

  8. HDU 4364——Matrix operation——————【模拟题】

    Matrix operation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU 4357——String change——————【规律题】

    String change Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. HDU 5371——Hotaru's problem——————【manacher处理回文】

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...