• 爬取猫眼电影top100的代码


    废话不说,代码附上:

    #encoding:utf-8
    import requests
    
    import re
    
    import json
    
    from multiprocessing import Pool #多线程模块
    
     
    
    #获取网页源代码
    
    def get_one_page(url):
    
        #添加头信息
    
        headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.3964.2 Safari/537.36'}
    
        html = requests.get(url,headers = headers)
    
        if html.status_code == 200:
    
            return html.text
    
        return None
    
     
    
    #根据正则表达式提取信息
    
    def parse_one_page(html):
    
        pattern = re.compile('<dd>.*?board-index.*?>(d+)</i>.*?title="(.*?)".*?data-src="(.*?)".*?star">(.*?)</p>.*?'
    
                             + 'releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
    
        data = pattern.findall(str(html))
    
        for item in data:
    
            yield{
    
                'top':item[0],
    
                'name':item[1],
    
                'image_src':item[2],
    
                'actor':item[3].strip()[3:],
    
                'releasetime':item[4].strip()[5:],
    
                'score':item[5] + item[6]
    
            }
    
    #将提取得数据写人文件
    
    def write_of_file(content):
    
        # 'a'保留原先的内容,在尾部写入,因为用的是for循环写入,如果用'w'写入只会保留最后一组的数据,或者早打开文件,写入完在关闭,encoding='utf-8'以utf-8编码
    
        with open('result.txt','a',encoding='utf-8') as f:
    
            #ensure_ascii = False使中文正常输出
    
            f.write(json.dumps(content,ensure_ascii = False) + '
    ')
    
            f.close()
    
    #主函数
    
    def main(offset):
    
        #要爬取的网址
    
        url = "http://maoyan.com/board/4?offset=" + str(offset)
    
        html = get_one_page(url)
    
        for item in parse_one_page(html):
    
            print(item)
    
            write_of_file(item)
    
     
    
    if __name__ == '__main__':
    
        #定义多线程
    
        pool = Pool()
    
        pool.map(main,[i*10 for i in range(10)])
    
  • 相关阅读:
    windows server 2008 R2 安装 sql server 2000 遇到的种种问题
    圆心的VPS记录信息
    无法访问.您可能没有权限使用网络资源.局域网无法访问共享,局域网无法访问打印机的一些方法
    win7 32位 fastcgi模式 运行php
    SpringBoot+JPA实例
    马斯洛的锤子论
    一道算法题
    MySQL源码解读之数据结构LF_HASH
    JS学习专辑外传(1)
    WPF 用DynamicResource动态样式切换
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10782118.html
Copyright © 2020-2023  润新知