博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
进程池与回调函数与正则表达式和re爬虫例子
阅读量:6985 次
发布时间:2019-06-27

本文共 1122 字,大约阅读时间需要 3 分钟。

# 使用进程池的进程爬取网页内容,使用回调函数处理数据,用到了正则表达式和re模块import refrom urllib.request import urlopenfrom multiprocessing import Pooldef get_page(url,pattern):    response=urlopen(url).read().decode('utf-8')    return pattern,response   # 返回正则表达式编译结果 网页内容def parse_page(info):    pattern,page_content=info    # 接收到正则表达式编译结果,与网页内容    res=re.findall(pattern,page_content)    # 调用re模块的方法,用正则匹配到网页的内容    for item in res:        dic={            'index':item[0].strip(),            'title':item[1].strip(),            'actor':item[2].strip(),            'time':item[3].strip(),        }        print(dic)if __name__ == '__main__':    regex = r'
.*?<.*?class="board-index.*?>(\d+).*?title="(.*?)".*?class="movie-item-info".*?

(.*?)

.*?

(.*?)

' pattern1=re.compile(regex,re.S) # 将正则表达式编译后存到变量中 url_dic={
'http://maoyan.com/board/7':pattern1} # 一个url对应一个正则 p=Pool() res_l=[] for url,pattern in url_dic.items(): res=p.apply_async(get_page,args=(url,pattern),callback=parse_page) res_l.append(res) for i in res_l: i.get()

 

转载于:https://www.cnblogs.com/whylinux/p/9839467.html

你可能感兴趣的文章
leetcode 29. Divide Two Integers
查看>>
axis调用webservice客户端开发
查看>>
Activiti5第八弹,ProcessEngineConfiguration和ProcessEngine
查看>>
细说C#多线程那些事 - 线程同步和多线程优先级
查看>>
单例模式
查看>>
发送验证码倒计时
查看>>
程序员看中的浏览器
查看>>
浙大pat甲级题目---1021. Deepest Root (25)
查看>>
CCRD_TOC_2008年第2期
查看>>
脊柱关节病外周关节滑膜高表达的RANK/RANKL/OPG系统与炎症呈部分分离
查看>>
vue Cli 脚手架的搭建
查看>>
springboot配置Filter的两种方法
查看>>
substringToIndex substringFromIndex
查看>>
开源方案搭建可离线的精美矢量切片地图服务-1.开篇(附成果演示地址)
查看>>
UVA 1613 K-Graph Oddity K度图着色 (构造)
查看>>
Docker版本(三)
查看>>
汇编小知识
查看>>
eclipse使用git提交本地项目,提交至远程github上
查看>>
out对象的使用
查看>>
python3随记——字符编码
查看>>