[Python36] 01 start

这篇博客以项目形式介绍Python基础知识和爬虫知识。作者通过编写获取豆瓣读书Top250榜单的程序作为起点,指出这比传统的"Hello,world!"更能展示Python语法特性。运行代码会显示Top250前25的内容,但需先安装requests和lxml库。程序在Macbook Air(M1)上运行时间为4.17秒。" 103455380,7699274,机器学习基础:从应用到模型训练,"['机器学习', '数据预处理', '线性回归', '回归问题', '分类问题']
摘要由CSDN通过智能技术生成

引言

    前段时间里,在各大社媒涌现出很多团队进行宣传并推荐各自Python课程,价格也是不等,内容有好有坏,吸收看个人。有的人可能会感到疑惑,通过一段时间的学习,课程的练习也能做出来,可是对编写程序还是一知半解。

    于是,我将开始开启一个小项目,以项目的形式进行Python相关知识的讲解。在这里,你可以学习到Python的基础知识,也可以通过该课程学习到爬虫的内容。

第一个Python程序

    如果你学习过其他的编程语言,一定会接触到 “Hello,world!” 程序。这个程序也是通往编程世界的通天塔。而在Python里面这个程序仅需要一个语句print("Hello,world!")即可完成。但是不足以完美的介绍出Python的语法特性。

    于是,我将Python的第一个程序调整为 “获取豆瓣读书Top250榜单”,后续内容也将根据这段代码进行深入讲解。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Edit in main.py
"""获取豆瓣读书top250信息"""

import time
import requests as req
from lxml import etree

start_time = time.perf_counter()    # 使用当前时间节点进行计数,初始化该变量为0
start = 0   # 游标
base_url = "https://book.douban.com/top250?start="  # 基础url,用于对url的拼接
headers = {
   
    # 请求头
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)"
                  "Chrome/102.0.5005.61 Safari/537.36"
}
result = []

while True:
    url = base_url + str(start)
    # url = f"{base_url}{start}"
    response = req.get(url, headers=headers)
    if response.status_code == 200:
        html = etree.HTML(response.text)
        titles = html.xpath("//tr[@class='item']//a/@title")
        descriptions = html.xpath("//tr[@class='item']//p[@class='pl']/text()")

        if not (titles and descriptions):
            print(f"运行结果为:{
     result}")
            end_time = time.perf_counter()  # 获取之前对时间节点到此时间节点的时间周期
            print(f"运行时间为:{
     end_time}")
            break

        for i, title in enumerate(titles):
            desc = descriptions[i].split(" / ")
            memo = {
   
                "rank": start + i + 1,
                "title": title,
                "author": desc[0],
                "press": desc[-3],
                "published_at": desc[-2],
                "price": desc[-1],
            }
            result.append(memo)
        start += 25
    else:
        print("请求错误")
        break

    通过python main.py运行即可获得相应Top250前25的内容。
    运行结果如下:

运行结果为:[{'rank': 1, 'title': '红楼梦', 'author': '[清] 曹雪芹 著', 'press': '人民文学出版社', 'published_at': '1996-12', 'price': '59.70元'}, {'rank': 2, 'title': '活着', 'author': '余华', 'press': '作家出版社', 'published_at': '2012-8-1', 'price': '20.00元'}, {'rank': 3, 'title': '1984', 'author': '[英] 乔治·奥威尔', 'press': '北京十月文艺出版社', 'published_at': '2010-4-1', 'price': '28.00'}, {'rank': 4, 'title': '百年孤独', 'author': '[哥伦比亚] 加西亚·马尔克斯', 'press': '南海出版公司', 'published_at': '2011-6', 'price': '39.50元'}, {'rank': 5, 'title': '三体全集', 'author': '刘慈欣', 'press': '重庆出版社', 'published_at': '2012-1-1', 'price': '168.00元'}, {'rank': 6, 'title': '飘', 'author': '[美国] 玛格丽特·米切尔', 'press': '译林出版社', 'published_at': '2000-9', 'price': '40.00元'}, {'rank': 7, 'title': '哈利·波特', 'author': 'J.K.罗琳 (J.K.Rowling)', 'press': '人民文学出版社', 'published_at': '2008-12-1', 'price': '498.00元'}, {'rank': 8, 'title': '房思琪的初恋乐园', 'author': '林奕含', 'press': '北京联合出版公司', 'published_at': '2018-2', 'price': '45.00元'}, {'rank': 9, 'title': '三国演义(全二册)', 'author': '[明] 罗贯中', 'press': '人民文学出版社', 'published_at': '1998-05', 'price': '39.50元'}, {'rank': 10, 'title': '动物农场', 'author': '[英] 乔治·奥威尔', 'press': '上海译文出版社', 'published_at': '2007-3', 'price': '10.00元'}, {'rank': 11, 'title': '福尔摩斯探案全集(上中下)', 'author': '[英] 阿·柯南道尔', 'press': '群众出版社', 'published_at': '1981-8', 'price': '53.00元/68.00元'}, {'rank': 12, 'title': '白夜行', 'author': '[日] 东野圭吾', 'press': '南海出版公司', 'published_at': '2013-1-1', 'price': '39.50元'}, {'rank': 13, 'title': '小王子', 'author': '[法] 圣埃克苏佩里', 'press': '人民文学出版社', 'published_at': '2003-8', 'price': '22.00元'}, {'rank': 14, 'title': '天龙八部', 'author': '金庸', 'press': '生活·读书·新知三联书店', 'published_at': '1994-5', 'price': '96.00元'}, {'rank': 15, 'title': '安徒生童话故事集', 'author': '(丹麦)安徒生', 'press': '人民文学出版社', 'published_at': '1997-08', 'price': '25.00元'}, {'rank': 16, 'title': '撒哈拉的故事', 'author': '三毛', 'press': '哈尔滨出版社', 'published_at': '2003-8', 'price': '15.80元'}, {'rank': 17, 'title': '杀死一只知更鸟', 'author': '[美] 哈珀·李', 'press': '译林出版社', 'published_at': '2012-9', 'price': '32.00元'}, {'rank': 18, 'title': '呐喊', 'author': '鲁迅', 'press': '人民文学出版社', 'published_at': '1973-3', 'price': '0.36元'}, {'rank': 19, 'title': '明朝那些事儿(1-9)', 'author': '当年明月', 'press': '中国海关出版社', 'published_at': '2009-4', 'price': '358.20元'}, {'rank': 20, &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值