加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

进程池与回调函数与正则表达式和re爬虫例子

发布时间:2020-12-14 06:14:13 所属栏目:百科 来源:网络整理
导读:# 使用进程池的进程爬取网页内容,使用回调函数处理数据,用到了正则表达式和re模块 import re from urllib.request import urlopen from multiprocessing import Pool def get_page(url,pattern): response =urlopen(url).read().decode( ‘ utf-8 ‘ ) ret
# 使用进程池的进程爬取网页内容,使用回调函数处理数据,用到了正则表达式和re模块

import re
from urllib.request import urlopen
from multiprocessing import Pool

def 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<dd>.*?<.*?class="board-index.*?>(d+)</i>.*?title="(.*?)".*?class="movie-item-info".*?<p class="star">(.*?)</p>.*?<p class="releasetime">(.*?)</p>
    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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读