Python  多进程爬虫实例

import json
import re
import time
from multiprocessing import Pool
import requests
from requests.exceptions import RequestException
from bs4 import BeautifulSoup 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):
data_list = []
soup = BeautifulSoup(html, "lxml")
index_list = soup.select('i.board-index')
img_list = [x['data-src'] for x in soup.findAll('img', {'class': 'board-img'})]
name_list = soup.select('p.name')
actor_list = soup.select('p.star')
time_list = soup.select('p.releasetime')
score_list = soup.select('p.score')
for i in range(len(index_list)):
data_list.append({
'index': index_list[i].get_text(),
'image': img_list[i],
'title': name_list[i].get_text(),
'actor': actor_list[i].get_text().strip(),
'time': time_list[i].get_text(),
'score': score_list[i].get_text()
})
return data_list def write_to_file(content):
with open('resul1t.txt', 'a', encoding='utf-8') as f:
f.write(json.dumps(content, ensure_ascii=False) + '\n')
f.close() def main(offset_list):
for offset in offset_list:
url = 'http://maoyan.com/board/4?offset=' + str(offset)
html = get_one_page(url)
if html:
for item in parse_one_page(html):
write_to_file(item) if __name__ == '__main__':
# pool = Pool()
# pool.map(main, [i * 10 for i in range(10)])
# pool.close()
# pool.join()
# main(1) offset_list = list(range(0, 100, 10)) # 多进程
p = Pool()
for index in range(5):
p.apply_async(main, args=(offset_list[index * 2:(index + 1) * 2],)) p.close()
p.join()

Python 多进程爬虫实例的更多相关文章

  1. python 多线程爬虫 实例

    多进程 Multiprocessing 模块 Process 类用来描述一个进程对象.创建子进程的时候,只需要传入一个执行函数和函数的参数即可完成 Process 示例的创建. star() 方法启动 ...

  2. python多进程通信实例分析

    操作系统会为每一个创建的进程分配一个独立的地址空间,不同进程的地址空间是完全隔离的,因此如果不加其他的措施,他们完全感觉不到彼此的存在.那么进程之间怎么进行通信?他们之间的关联是怎样的?实现原理是什么 ...

  3. python scrapy 爬虫实例

    1 创建一个项目 scrapy startproject basicbudejie 2 编写爬虫 import scrapy class Basicbudejie(scrapy.Spider): na ...

  4. Python小爬虫实例

    有几个注意点: # -*- coding: utf-8 -*- # func passport jw.qdu.edu.cn import re import urllib# python3后urlli ...

  5. python 微信爬虫实例

    单线程版: import urllib.request import urllib.parse import urllib.error import re,time headers = (" ...

  6. Python 爬虫实例

    下面是我写的一个简单爬虫实例 1.定义函数读取html网页的源代码 2.从源代码通过正则表达式挑选出自己需要获取的内容 3.序列中的htm依次写到d盘 #!/usr/bin/python import ...

  7. Python多进程并发(multiprocessing)用法实例详解

    http://www.jb51.net/article/67116.htm 本文实例讲述了Python多进程并发(multiprocessing)用法.分享给大家供大家参考.具体分析如下: 由于Pyt ...

  8. Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取

    很多网页的信息都是通过异步加载的,本文就举例讨论下此类网页的抓取. <工作细胞>最近比较火,bilibili 上目前的短评已经有17000多条. 先看分析下页面 右边 li 标签中的就是短 ...

  9. Python爬虫实例:爬取猫眼电影——破解字体反爬

    字体反爬 字体反爬也就是自定义字体反爬,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的. 现在貌似不少网 ...

随机推荐

  1. 我的windows开发环境设定与日常使用指南

    目录 开发相关的软件包安装.设定 Visual Studio 默认设定 鼠标右键添加"在此处打开cmd"选项 git gvim notepad++ VSCode-Insider C ...

  2. go test benchmark

    Benchtest的简单使用 一个简单的benchtest用例 // 以BenchmarkXXX类似命名,并传入b *testing.B 参数 func BenchmarkLoopSum(b *tes ...

  3. mysql创建临时表,将查询结果插入已有的表

    A.临时表再断开于mysql的连接后系统会自动删除临时表中的数据,但是这只限于用下面语句建立的表:1)定义字段  CREATE TEMPORARY TABLE tmp_table (      nam ...

  4. 自定义微信小程序swiper轮播图面板指示点的样式

    微信小程序的swiper组件是滑块视图容器,也就是说平常我们看到的轮播图就可以用它来做,不过这个组件有很多样式是固定的,但是,有时候我们的设计稿的面板指示点是需要个性化的,那么如何去修改swiper组 ...

  5. LGOJP3193 [HNOI2008]GT考试

    \(f[i][j]\)表示当前摆放到第\(i\)位,然后当前的匹配长度为\(j\) \(f[i][j]=\sum {f[i][k]*g[k][j]}\) \(g[i][j]\)表示将长度为\(i\)的 ...

  6. Photon介绍与安装

    Photon光子引擎是一款实时的Socket服务器和开发框架,快速.使用方便.容易扩展. 服务端架构在windows系统平台上,采用C#语言编写. 客户端SDK提供了多种平台的开发API,包括DotN ...

  7. tasklist /m

    \>tasklist /m explorer*映像名称                       PID 模块                                        = ...

  8. redux有价值的文档

    使用 Redux 管理状态,第 1 部分 https://www.ibm.com/developerworks/cn/web/wa-manage-state-with-redux-p1-david-g ...

  9. vscode React编程配置

    2.添加RN开发插件 React Native Tools:微软官方出的ReactNative插件,非常好用Reactjs code snippets:react的代码提示,如componentWil ...

  10. eclipse中的maven插件

    导入一个maven项目,一直报错:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的错误 Description ...