xpath爬取当当网
2021/9/8 6:08:16
本文主要是介绍xpath爬取当当网,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
xpath爬取当当网
import requests from lxml import etree #from itertools import chain #import json # 利用接口读取访问json文件 import time ''' xpath爬取当当网图书畅销榜 ''' def main(): #r = [] d = {} for i in range(1,26): resp = requests.get('http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-recent7-0-0-1-'+str(i)) res_xpath = etree.HTML(resp.text) title = res_xpath.xpath('///div[@class="name"]/a/text()') # 书名 author = res_xpath.xpath('///div[@class="publisher_info"]/a[@title]/text()') # 作者 price = res_xpath.xpath('///div[@class="price"]/p/span[@class="price_n"]/text()') # 价格 #publisher = res_xpath.xpath('///div[@class="publisher_info"]/a[@href]/text()') # 出版社 star = res_xpath.xpath('///div[@class="star"]/span[@class="tuijian"]/text()') # 推荐度 date = res_xpath.xpath('///div[@class="publisher_info"]/span/text()') # 出版日期 d.setdefault('_title',[]).append(title) # 格式化字典 d.setdefault('_author',[]).append(author) d.setdefault('_price',[]).append(price) d.setdefault('_star',[]).append(star) d.setdefault('_date',[]).append(date) print('第%s页爬取完成!' % i) #r = list(chain(r,rst)) time.sleep(1) print(d) # with open('D:/r/book4.txt','a+',encoding='utf-8') as f: # for i in range(0,len(r)): # f.write(str(i+1)+'. '+r[i]+'\n') # # data_model = json.loads(i) # # for news in data_model: # # print(news) # # #print(rst) # print('已全部写入!') if __name__ == '__main__': main()
这篇关于xpath爬取当当网的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)