# -*- coding: utf-8 -*-
import requests
from multiprocessing import Pool
from requests.exceptions import RequestException
import re
import json def get_one_page(url):
"""
爬取每个页面
:param url: 爬取url地址
:return: 返回网页内容
"""
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
return None
except RequestException:
return None def parse_one_page(html):
"""
处理筛选网页内容中需要的信息
:param html: 网页内容
:return: 字典
"""
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)
for item in items:
yield {
'index': item[0],
'image': 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):
"""
将结果数据写入文件
:param content: 需要写入文件的内容
:return:
"""
with open('result.txt', 'a', encoding='utf-8') as f:
f.write(json.dumps(content, ensure_ascii=False) + "\n")
f.close() def main(offset):
"""
主函数
:param offset: offset值,用于构造url
:return:
"""
url = "http://maoyan.com/board/4?offset=" + str(offset)
html = get_one_page(url)
parse_one_page(html)
for item in parse_one_page(html):
print(item)
write_to_file(item) if __name__ == '__main__':
# for i in range(10):
# main(i*10)
pool = Pool()
pool.map(main, [i*10 for i in range(10)])

【来自天善智能】:https://edu.hellobi.com/course/156/play/lesson/2453

崔大师的代码看着就是舒服。。。。

Python爬取猫眼top100排行榜数据【含多线程】的更多相关文章

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

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

  2. 记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中

    猫眼票房排行榜页面显示如下: 注意右边的票房数据显示,爬下来的数据是这样显示的: 网页源代码中是这样显示的: 这是因为网页中使用了某种字体的缘故,分析源代码可知: 亲测可行: 代码中获取的是国内票房榜 ...

  3. python爬虫:爬取猫眼TOP100榜的100部高分经典电影

    1.问题描述: 爬取猫眼TOP100榜的100部高分经典电影,并将数据存储到CSV文件中 2.思路分析: (1)目标网址:http://maoyan.com/board/4 (2)代码结构: (3) ...

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

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

  5. 利用python爬取58同城简历数据

    利用python爬取58同城简历数据 利用python爬取58同城简历数据 最近接到一个工作,需要获取58同城上面的简历信息(http://gz.58.com/qzyewu/).最开始想到是用pyth ...

  6. 抓取猫眼TOP100的数据

    import requests import re import json from multiprocessing import Pool from multiprocessing import M ...

  7. 使用Beautiful Soup爬取猫眼TOP100的电影信息

    使用Beautiful Soup爬取猫眼TOP100的电影信息,将排名.图片.电影名称.演员.时间.评分等信息,提取的结果以文件形式保存下来. import time import json impo ...

  8. 手把手教你使用Python爬取西刺代理数据(下篇)

    /1 前言/ 前几天小编发布了手把手教你使用Python爬取西次代理数据(上篇),木有赶上车的小伙伴,可以戳进去看看.今天小编带大家进行网页结构的分析以及网页数据的提取,具体步骤如下. /2 首页分析 ...

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

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

随机推荐

  1. codeM 2018 资格赛

    比赛链接:https://www.nowcoder.com/activity/2018codem/index?from=meituan 1.下单 给定若干商品,可以选择打折.满减两种方式. #incl ...

  2. 倒计时实现两种方法-NSTimer/GCD

    #import "ViewController.h" @interface ViewController () @property (nonatomic ,strong)UIBut ...

  3. CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别

    这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威: Creates a thread to execute within the virtual address space o ...

  4. Ubuntu下看不见pthread_create(安装pthread线程库)

    使用下面的命令就可以了! sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev 然后在用man -k pthre ...

  5. kubernetes删除pod失败

    一.概述 k8s中删除pod失败,可能是该pod有rc,rs上层控制,而且很有可能,所以删除上层对应的rc,rs,deployment即可: 删除的方法: 1.直接删除rc,rs,deployment ...

  6. Android Studio 常见问题汇总

    一.字体大小问题 在android studio的使用过程中没有发现类似于Eclipse中的font选项,调节字体大小方法如下: 1.File---- >Settings,找到Editor 2. ...

  7. 【Android】Android 8种对话框(Dialog)

    1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮).列表.单选.多选.等待.进度条.编辑.自定义等多种形式,将在第2部分介绍 ...

  8. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  9. Error Code: 1030. Got error -1 from storage engine

    这个问题通常是数据库可以建表,旧表可以插入数据,正常:可是新表无法插入数据,无法改名等操作: 先从文件权限找方法,没法解决: 在网上搜了一通,大家都说的磁盘满了,但是我们的磁盘还空着呢! 后来,发现! ...

  10. Linux Crontab内环境变量与Shell环境变量的关系及解决问题的办法

    为了定时监控Linux系统CPU.内存.负载的使用情况,写了个Shell脚本,当达到一定值得时候,发送邮件通知.需要用到Crontab的定时任务去执行这个脚本,但是发现通过命令(./test.sh)执 ...