【练习】爬当当网——字典嵌套元组(老师版)
import requests
from lxml import html
import pandas as pd
# 9787115428028(需要用到的国际标准书号)
def spider(isbn):
# 用国际标准书号的方式获取查询网站的链接
url = "http://search.dangdang.com/?key={}&act=input".format(isbn)
print('此国际标准书号的链接为:', url)
# 获取网页源代码
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'}
# 欺骗服务器
html_data = requests.get(url, headers=headers).text
# 使用xpath语法提取我们需要的内容
selector = html.fromstring(html_data)
ul_list = selector.xpath('//div[@id="search_nature_rg"]/ul/li') # 以li为出发点
print('有{}家商家出售此书'.format(len(ul_list)))
book_info_list = [] #定义一个空列表,利用for...in循环往列表里添加元素
for li in ul_list:
# 获取书的标题
title = li.xpath('a/@title')[0]
# 获取书的链接
link = li.xpath('a/@href')[0]
# 获取书的价格
price = li.xpath('p[@class="price"]/span[@class="search_now_price"]/text()')[0]
price = price.replace('¥', '')
# 获取销售此书的商家
store = li.xpath('p[@class="search_shangjia"]/a/text()')
if len(store) == 0:
store = '当当自营'
else:
store = store[0]
# 往book_info_list列表中添加元素
book_info_list.append(
{"title": title, "price": price, "link": link, "store": store}
)
print('排序前:', book_info_list)
for book in book_info_list:
print(book)
print("#######################################")
print("#######################################")
print("#######################################")
# 定义匿名函数实现按价格来排序
book_info_list.sort(key=lambda x: float(x['price'])) # 记得float()强制转换原来的字符串型价格数据
print('排序后:', book_info_list)
for book in book_info_list:
print(book)
# 转化成dataframe格式
df = pd.DataFrame(book_info_list)
# 存储成csv格式的文件(csv:逗号分隔符文件)
df.to_csv('当当图书信息.csv')
isbn = input("请输入你要查询的书号:")
spider(isbn)
请输入你要查询的书号:9787115428028
此国际标准书号的链接为:http://search.dangdang.com/?key=9787115428028&act=input
有60家商家出售此书
排序前:[{'title': ' Python编程 从入门到实践', 'price': '62.00', 'link': 'http://product.dangdang.com/24003310.html', 'store': '当当自营'}, {'title': ' Python编程从入门到实践+零基础入门学习Python+Python基础教程第三版 Python编程入门书籍全3册P', 'price': '237.50', 'link': 'http://product.dangdang.com/1427665587.html', 'store': '博鸿文化图书专营店'}......]
{'title': ' Python编程 从入门到实践', 'price': '62.00', 'link': 'http://product.dangdang.com/24003310.html', 'store': '当当自营'}
{'title': ' Python编程从入门到实践+零基础入门学习Python+Python基础教程第三版 Python编程入门书籍全3册P', 'price': '237.50', 'link': 'http://product.dangdang.com/1427665587.html', 'store': '博鸿文化图书专营店'}
{'title': ' Python Java C 语言从入门到精通三本套计算机核心编程基础教程书籍 零基础自学电脑程序设计开发进阶实践教材', 'price': '194.50', 'link': 'http://product.dangdang.com/1405219788.html', 'store': '艺凡图书专营店'}
{'title': ' Python编程(从入门到实践) 附程序员面试手册', 'price': '73.10', 'link': 'http://product.dangdang.com/1541170418.html', 'store': '土星图书专营店'}
{'title': ' Python Java C 语言从入门到精通三本套计算机核心编程基础教程书籍 零基础自学电脑程序设计开发进阶实践教材', 'price': '194.70', 'link': 'http://product.dangdang.com/1354186099.html', 'store': '泊霖文轩图书专营店'}
......
{'title': ' 正版现货 Python编程从入门到实践 python基础教程 Python核心编程 Python程序设计 python', 'price': '88.00', 'link': 'http://product.dangdang.com/1328174548.html', 'store': '育博彦图书专营店'}
{'title': ' 【全2册】Python编程 从入门到实践+笨办法学Python 3 绝技核心编程基础视频教程网络爬虫', 'price': '121.40', 'link': 'http://product.dangdang.com/1353670125.html', 'store': '世纪书缘图书专营店'}
{'title': ' 【套装3本】Python编程从入门到实践+Python核心编程 第3版+Python 3标准库', 'price': '382.00', 'link': 'http://product.dangdang.com/1306734922.html', 'store': '阅微阁图书专营店'}
{'title': ' 现货 自我进化 发现你的创新力 蒂娜齐莉格 著', 'price': '37.00', 'link': 'http://product.dangdang.com/1330247877.html', 'store': '童心悦美图书专营店'}
{'title': ' python基础教程 零基础学python3.5编程从入门到实践 精通计算机程序设计pathon核心技术网络爬虫书籍', 'price': '138.00', 'link': 'http://product.dangdang.com/1324822717.html', 'store': '嘉博致远图书专营店'}
#######################################
#######################################
#######################################
排序后:[{'title': ' 现货 自我进化 发现你的创新力 蒂娜齐莉格 著', 'price': '37.00', 'link': 'http://product.dangdang.com/1330247877.html', 'store': '童心悦美图书专营店'}, {'title': ' Python编程 从入门到实践 python核心编程基础教程数据分析快速上手入门学习手册网络爬虫', 'price': '59.80', 'link': 'http://product.dangdang.com/1100731314.html', 'store': '翰诚图书专营店'}......
{'title': ' 现货 自我进化 发现你的创新力 蒂娜齐莉格 著', 'price': '37.00', 'link': 'http://product.dangdang.com/1330247877.html', 'store': '童心悦美图书专营店'}
{'title': ' Python编程 从入门到实践 python核心编程基础教程数据分析快速上手入门学习手册网络爬虫', 'price': '59.80', 'link': 'http://product.dangdang.com/1100731314.html', 'store': '翰诚图书专营店'}
{'title': ' Python编程 从入门到实践', 'price': '62.00', 'link': 'http://product.dangdang.com/24003310.html', 'store': '当当自营'}
{'title': ' 现货Python编程快速上手 让繁琐工作自动化 Python从入门到精通 Python编程书籍 计算机程序设计编程书籍', 'price': '62.00', 'link': 'http://product.dangdang.com/1540015344.html', 'store': '鹰辰文化图书专营店'}
{'title': ' Python编程(从入门到实践) 附程序员面试手册', 'price': '73.10', 'link': 'http://product.dangdang.com/1541170418.html', 'store': '土星图书专营店'}
......
{'title': ' Python编程-从入门到实践+Python核心编程+利用Python进行数据分析 python程序设计基础教程 py', 'price': '276.00', 'link': 'http://product.dangdang.com/1526832163.html', 'store': '金果果图书专营店'}
{'title': ' 【套装3本】Python编程从入门到实践+Python核心编程 第3版+Python 3标准库', 'price': '382.00', 'link': 'http://product.dangdang.com/1306734922.html', 'store': '阅微阁图书专营店'}
{'title': ' 【套装3本】Python编程从入门到实践+Python核心编程 第3版+Python 3标准库', 'price': '386.00', 'link': 'http://product.dangdang.com/1441429764.html', 'store': '三味书屋图书专营店'}
{'title': ' 【套装9本】C++ PrimerPlus第六版中文版C#图解教程Java编程思想第四版python编程从入门到实践鸟哥', 'price': '798.00', 'link': 'http://product.dangdang.com/1398453504.html', 'store': '阅微阁图书专营店'}
{'title': ' 【套装9本】C++ PrimerPlus第六版中文版C#图解教程Java编程思想第四版python编程从入门到实践鸟哥', 'price': '820.00', 'link': 'http://product.dangdang.com/1536289922.html', 'store': '阅微阁图书专营店'}
.csv文件会保存到本地,效果如下: