Python 多进程爬虫实例
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 多进程爬虫实例的更多相关文章
- python 多线程爬虫 实例
多进程 Multiprocessing 模块 Process 类用来描述一个进程对象.创建子进程的时候,只需要传入一个执行函数和函数的参数即可完成 Process 示例的创建. star() 方法启动 ...
- python多进程通信实例分析
操作系统会为每一个创建的进程分配一个独立的地址空间,不同进程的地址空间是完全隔离的,因此如果不加其他的措施,他们完全感觉不到彼此的存在.那么进程之间怎么进行通信?他们之间的关联是怎样的?实现原理是什么 ...
- python scrapy 爬虫实例
1 创建一个项目 scrapy startproject basicbudejie 2 编写爬虫 import scrapy class Basicbudejie(scrapy.Spider): na ...
- Python小爬虫实例
有几个注意点: # -*- coding: utf-8 -*- # func passport jw.qdu.edu.cn import re import urllib# python3后urlli ...
- python 微信爬虫实例
单线程版: import urllib.request import urllib.parse import urllib.error import re,time headers = (" ...
- Python 爬虫实例
下面是我写的一个简单爬虫实例 1.定义函数读取html网页的源代码 2.从源代码通过正则表达式挑选出自己需要获取的内容 3.序列中的htm依次写到d盘 #!/usr/bin/python import ...
- Python多进程并发(multiprocessing)用法实例详解
http://www.jb51.net/article/67116.htm 本文实例讲述了Python多进程并发(multiprocessing)用法.分享给大家供大家参考.具体分析如下: 由于Pyt ...
- Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取
很多网页的信息都是通过异步加载的,本文就举例讨论下此类网页的抓取. <工作细胞>最近比较火,bilibili 上目前的短评已经有17000多条. 先看分析下页面 右边 li 标签中的就是短 ...
- Python爬虫实例:爬取猫眼电影——破解字体反爬
字体反爬 字体反爬也就是自定义字体反爬,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的. 现在貌似不少网 ...
随机推荐
- Linux命令passwd
passwd 简单说明:passwd命令的用法也很多,我们只选如下的几个参数加以说明:想了解更多,请参考man passwd或passwd --help :passwd [OPTION...] pas ...
- 浅谈Python设计模式 - 适配器模式
声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 从本篇便开始介绍结构型设计模式,而适配器设计模式便是该类设计模式的一种,那么什么 ...
- JQuery-zTree.js使用范例
JQuery-zTree.js使用范例 实现Tree树的插件很多,比如常见的UI:Layui.ElementUI.iView ... .这里我们介绍一个小巧的构建Tree树的插件 zTree.js z ...
- mklink 文件夹链接 windows系统
MS文档 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/mklink 命令参数 mkl ...
- Tensorflow简单实践系列(三):图和会话
当执行一个 TensorFlow 函数的时候,并不会马上执行运算,而是把运算存储到一个称为“图”(graph)的数据结构里面. 图存储的各种运算,只有在会话(session)里执行图,才会真正地执行. ...
- dfs 正则表达式
192. 通配符匹配 中文 English 判断两个可能包含通配符“?”和“*”的字符串是否匹配.匹配规则如下: '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个串完 ...
- python基础:zip和dict详解
一.zip函数:接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表. 1.示例1: x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zi ...
- PureComponent & shouldComponentUpdate
Called to determine whether the change in props and state should trigger a re-render. Component alwa ...
- 2017icpc beijing-I题-Colored Nodes
题意 给定一个n个点m条边的无向图,一开始点i的颜色为i,在第i+kn秒开始时,与节点i相邻的节点会被染成i的颜色(k为自然数) 定义D(i,j)第j秒结束时颜色为i的节点个数,求: $F(i)=\l ...
- @EnableFeignClients 客户端详细
在Spring cloud应用中,当我们要使用feign客户端时,一般要做以下三件事情 : 1.使用注解@EnableFeignClients启用feign客户端: 示例 : @SpringBootA ...