zoukankan      html  css  js  c++  java
  • 抓取猫眼电影前100

    import json
    import requests
    import re
    import time
    from requests.exceptions import RequestException

    def get_one_page(url):
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'
            }
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                print(response.text)
                return response.text  # 使得get_one_page()函数输出是一个文本
            return None
        except RequestException:
            return None

    def parse_one_page(html):
        pattern = re.compile(
            '<dd>.*?board-index.*?>(.*?)</i>.*?name.*?a.*?>(.*?)</a>.*?star.*?>(.*?)</p>.*?releasetime.*?>(.*?)</p>.*?'
            'integer.*?>(.*?)</i>.*?fraction.*?>(.*?)</i>.*?</dd>',
            re.S)  # 正则表达式获取需要保存的东西编译成正则表达式对象
        items = re.findall(pattern, html)  # 遍历html文件中的所有pattern正则表达式对象
        for item in items:  # 把提取的对象装入字典中
            yield {
                'index': item[0],
                'title': item[1],
                'actor': item[2].strip()[3:],
                'time': item[3].strip()[5:],
                'score': item[4] + item[5]
            }


    def write_to_file(content):  # 把文件写入并保存在result.tx + ' ')
        with open('result.txt', 'a', encoding='utf-8') as f:
            f.write(json.dumps(content, ensure_ascii=False) + ' ')


    def main(offset):  # 遍历TOP100的电影的所有网址
        url = 'http://maoyan.com/board/4?offset=' + str(offset)  # 接收一个偏移量offset
        html = get_one_page(url)
        for item in parse_one_page(html):
            print(item)
            write_to_file(item)


    if __name__ == '__main__':  # 创建一个偏移量offset
        for i in range(10):
            main(offset=i * 10)
            time.sleep(1)
  • 相关阅读:
    7款强大的Javascript网格插件推荐 狼人:
    90后英国中学生建立黑客社交网 涉案金额达1.8亿元 狼人:
    好的代码里只要一个return语句 狼人:
    一个月内从零开始做webOS开发人员 狼人:
    FireFox 5开发计划曝光 内嵌PDF阅读器(组图) 狼人:
    谷歌用户体验设计准则 狼人:
    15个编程好习惯 狼人:
    Debain/ArchLinux/Gentoo 等将合并为超级Linux 狼人:
    别说你不知IE9正式版浏览器小技巧9则 狼人:
    Firebug1.8a1发布 新功能、新架构 狼人:
  • 原文地址:https://www.cnblogs.com/jestin/p/12911360.html
Copyright © 2011-2022 走看看