#!/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. python 格式化时间含中文报错: 'locale' codec can't encode character '\u5e74'

    执行下面代码报错: UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal ...

  2. python groupby的小技巧

    df.groupby('col',as_index=False),agg(ufunk)#as_index=False,可以消除层次索引 更多grouby的用法 http://blog.csdn.net ...

  3. Android中的时间格式的校验

    public class MainActivity extends Activity implements OnClickListener{ private Button btn1; private ...

  4. Flag-2019上半年

    1.坚持做博客记录,把日常学习到的技能以日记的形势进行呈现,每周进行总结: 2.2月底完成吴老师的机器学习视频课程: 3.6月底完成吴老师的深度学习视频课程: 相信自己,可以的,半年后见!

  5. drop_caches控制page cache

    drop_caches Writing to this will cause the kernel to drop clean caches, as well as reclaimable slab ...

  6. Kudu-Impala集成特性

    不多说,直接上干货! Kudu-Impala 集成特性 CREATE / ALTER / DROP TABLE Impala 支持使用 Kudu 作为持久层来 creating(创建),alterin ...

  7. Day6上 括号匹配专项

    滑稽的题 T1 #include<iostream> #include<cstring> #include<queue> #include<algorithm ...

  8. Devexpress Xtrareport 创建主从报表

    效果 xtrareport 布局 From 代码 private DataSet Getdata() { DataSet ds = new DataSet(); //config配置字符串 strin ...

  9. DataGridView进度条列 C# WinForm

    先看看效果,如果感兴趣,继续往下看…… 效果如下图所示: DataGridView里没有Pragress列,但有Image列,有了它我们可以自己绘图来实现进度条.其实实现起来并不困难. 首先在实体类增 ...

  10. 命令“mkdir "xxx" xcopy "xxx" "xxx" /S /E /C /Y”已退出,代码为 9009。

    前几天公司来了个新同事,使用的VS2013,但我们的所有项目都是使用VS2012创建的,我想用13打开应该没有什么问题.昨天新同事修改完代码提交后,我获取完成后无法编译成功,提示: 错误 3 命令“m ...