zoukankan      html  css  js  c++  java
  • Python 爬虫之第一次接触

    爬豆瓣网电影TOP250名单 ------- 代码未写完,等待更新

    import requests
    from requests.exceptions import RequestException
    import re
    import json
    
    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):
        pattern = re.compile("", re.S)#笔记本屏幕太小看不过来
        items = re.findall(pattern, html)
    
        for item in items:#把爬取的信息进行格式化
            yield{
                "":item[0],
                "":item[1],
            }
    
    
    def write_to_file(content):
        with open("result.txt", "a", encoding="utf-8") as f:
            f.write(json.dumps(content, ensure_ascii=False) + "
    ")
            f.close()
    
    def main():
        url = "https://movie.douban.com/top250"
        html = get_one_page(url)
        # parse_one_page(html)
        for item in parse_one_page(html):
            print(item)
            write_to_file(item)
    
    if __name__ == "__main__":
        main()
    
    #这段代码没有加RE的匹配方式(笔记本屏幕小)
    #第二个这段代码只能匹配一页的东西,自己发挥想象把
  • 相关阅读:
    python_异常处理
    python_类与对象
    函数
    字符串(查找,替换,分割)
    容器类型的数据
    条件语句
    关于WinSock编程的多线程控制
    利用Delphi编写Socket通信程序
    SQL Server数据库开发的二十一条军规
    SQL Server中的日期格式化
  • 原文地址:https://www.cnblogs.com/GhostCatcg/p/8099660.html
Copyright © 2011-2022 走看看