Python网络爬虫:抓取书籍排行榜 并生成HTML网页
Hello,大家好,我是wangzirui32,我们来学习如何抓取书籍排行榜,并生成HTML网页,开始学习吧!
文章目录
- 1. 分析HTML
- 2. 爬虫程序
- 3. 生成HTML网页
- 3.1 render.py
- 3.2 books_template.html
1. 分析HTML
打开榜单链接,可以看到,所有的书籍数据均在一个ul
下的li
标签中。
可以看到,红框标注了我们需要的数据,定位如下:
number 书籍排行 li中第一个div标签
name 书籍名称 class属性为"name"的div中的a的title属性
link 书籍链接 class属性为"name"的div中的a的href属性
comments 书籍评论数 class属性为"star"的div中的a标签
support 书籍推荐率 class属性为"tuijian"的span标签
price 书籍价格 class属性为"price_n"的span标签
再来看网址的变化,第一页的网址为:
http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1
第二页的网址为:
http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-2
只要改变末尾数字,就可以实现多页的爬取。
2. 爬虫程序
请安装这些第三方库,方可开启爬虫之旅:
pip install requests bs4
代码如下:
import requests
from bs4 import BeautifulSoup as bs
import jsondef get_page(page):"""根据页数获取HTML"""url = "http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-{}".format(page)response = requests.get(url)return response.textdef parse_page(html):soup = bs(html, "html.parser")books_li = soup.find("ul", {"class": "bang_list clearfix bang_list_mode"}).find_all("li")books = []for li in books_li: # 提取并保存信息a = li.find("div", {"class": "name"}).anumber = li.div.text[:-1] # 去掉末尾“.”name = a.get("title")link = a.get("href")support_rate = li.find("span", {"class": "tuijian"}).textcomments = li.find("div", {"class": "star"}).a.text.replace("条评论", "") # 得到评论数字price = li.find("span", {"class": "price_n"}).text[1:] # 去掉价格符号books.append({"number": number,"name": name,"link": link,"support": support_rate,"comments": comments,"price": price})return booksdef main():"""入口程序"""page = 3 # 爬取页数为3books = []for i in range(1, page+1):html = get_page(i)books += parse_page(html)# 保存数据with open("data.json", "w", encoding="UTF-8") as f:json.dump(books, f, ensure_ascii=False)if __name__ == '__main__':main()
运行爬虫,可以看到data.json
文件在目录下生成。
3. 生成HTML网页
请在同一个目录下创建render.py
和books_template.html
。
请安装jinja2
:
pip install jinja2
3.1 render.py
from jinja2 import Template
import jsondef read_data():"""读取数据"""with open("data.json", "r", encoding="UTF-8") as f:data = json.load(f)return datadef read_template():"""读取HTML模板"""with open("books_template.html", encoding="UTF-8") as f:html = f.read()return htmldef render_template(html, data):"""渲染模板"""template = Template(html)result = template.render(data=data)return resultdef main():"""入口函数"""data = read_data()html = read_template()with open("books.html", "w", encoding="UTF-8") as f:f.write(render_template(html, data))if __name__ == '__main__':main()
3.2 books_template.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>书籍排行榜</title><!-- 引入Boostarp3.4.1 --><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"><style>body {padding-left: 20px;padding-right: 20px;}</style>
</head>
<body><h1 style="text-align: center;">书籍排行榜</h1><table class="table table-striped table-bordered table-hover table-condensed"><thead><tr><th>排行</th><th>书名</th><th>推荐率</th><th>评论数</th><th>价格</th></tr></thead><tbody>{% for i in data %}<tr><td>{{i.number}}</td><td><a href="{{i.link}}">{{i.name}}</a></td><td>{{i.support}}</td><td>{{i.comments}}条</td><td>${{i.price}}元</td></tr>{% endfor %}</tbody></table>
</body>
</html>
好了,今天的课程就到这里,我是wangzirui32,喜欢的可以点个收藏和关注,我们下次再见!
Python网络爬虫:抓取书籍排行榜 并生成HTML网页相关推荐
- python爬虫微信朋友圈怎么发文字_如何利用Python网络爬虫抓取微信朋友圈的动态(上)...
今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...
- python抓取微信朋友圈动态_2018最全如何利用Python网络爬虫抓取微信朋友圈的动态...
今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...
- 2018最全如何利用Python网络爬虫抓取微信朋友圈的动态
今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...
- 如何利用Python网络爬虫抓取微信朋友圈的动态(上)
今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...
- python展示全部好友_利用Python网络爬虫抓取微信好友的签名及其可视化展示
前几天给大家分享了如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化,利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,以及利用Python网络爬虫抓取微信好友的所 ...
- python爬虫好友聊天记录_利用Python网络爬虫抓取微信好友的签名及其可视化展示...
前几天给大家分享了如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化,利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,以及利用Python网络爬虫抓取微信好友的所 ...
- python爬虫能爬取微信密码吗_如何利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例...
今天我们继续focus on微信,不过这次给大家带来的是利用Python网络爬虫抓取微信好友总数量和微信好友男女性别的分布情况.代码实现蛮简单的,具体的教程如下. 相信大家都知道,直接通过网页抓取微信 ...
- python爬虫好友图片_利用Python网络爬虫抓取微信好友的签名及其可视化展示
前几天给大家分享了如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化,利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,以及利用Python网络爬虫抓取微信好友的所 ...
- 利用python从网络上爬取图片_一篇文章教会你利用Python网络爬虫抓取王者荣耀图片...
点击上方"IT共享之家",进行关注 回复"资料"可获赠Python学习福利 [一.项目背景] 王者荣耀作为当下最火的游戏之一,里面的人物信息更是惟妙惟肖,但受到 ...
最新文章
- 2020年春季信号与系统试卷批改
- Nuget很慢,我们该怎么办
- zabbix监控规划及实施
- (AIDE)Android Eclipse JNI 调用 .so文件加载问题
- java在SAE中使用的相关问题总结
- Divide by three, multiply by two CodeForces - 977D (思维排序)
- 天天Linux-编译和调试程序
- C++总结笔记(十二)—— 智能指针
- Apache Camel中的断路器模式
- linux环境变量自动配置,Linux进入系统时自动配置
环境变量的要领
- linux内核PID管理
- Python处理mongo结果中的ObjectId类型为字符串
- Spark--安装和配置遇到的所有问题
- 哪些多年前已从iBoxDB内核中移出的功能
- LINUX用终端命令调用打印机属性
- StanfordDB class自学笔记 (6) 关系代数
- 背景建模之codebook算法
- 人生的智慧——叔本华
- PRD(Product Requirement Document,产品需求文档)模板
- Capture Modules 车载网络报文捕获(监听)模块(低时延、802.1AS时钟同步)