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. linux解压缩的常用命令

    1.解包:tar xvf filename.tar, 打包: tar cvf filename DirName 2.解压:gunzip filename.gz, tar zxvf filename.t ...

  2. python 导入导出依赖包命令

    程序中必须包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号.如果 要在另一台电脑上重新生成虚拟环境,这个文件的重要性就体现出来了,例如部署程序时 使用的电脑.pip ...

  3. 折腾deepin修改终端语言

    原创作品,作者是博客园sogeisetsu,转载请注明来源sogeisetsu.cnblogs.com 唉-都怪当初没学扎实,改个终端语言花费了半天. 首先,介绍一下我的情况 有两个用户,一个是roo ...

  4. Show which git tag you are on?

    git查看当前代码是在那个tag? reference: https://stackoverflow.com/questions/3404936/show-which-git-tag-you-are- ...

  5. python应用-n颗骰子的和出现的次数

    from random import randint def roll_dice(n): total=0 for _ in range (n): num=randint(1,6) total+=num ...

  6. JSP中EL表达式的比较符号、字符串比较

    在EL表达式中我们可以使用运算符以达到我们想要的结果,运算符按作用分为以下几种: 1.算术运算符 +  例如:${6+6} .注意:在EL表达式中的‘+’只有数学运算的功能,没有连接符的功能,它会试着 ...

  7. solr根据id删除索引

    删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...

  8. UNICODE和UCS

    Unicode是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案.目前的Unicode字符分为17组编排,0x0000 至 0xFFFF,每组称为平面(Plane),而每平面拥有65536个码 ...

  9. Spring Data JPA:关联映射操作

    1.一对一的关系关联 需求:用户和角色一对一关联 package com.example.jpa.pojo; import javax.persistence.*; @Entity @Table(na ...

  10. 小程序&app 注册登录、绑定

    前段时间开发中的一款产品,有小程序和app:小程序直接微信登录,app使用手机号+验证码注册,手机号+验证码/密码登录. 用户使用其中一套账号密码即可正常使用,不强制要求完善另一套账号.为避免同一用户 ...