礼貌!快来指点指点!零基础自学.附上代码求指点🙏
import requests
from lxml import etree
import xlwt
urls = ('9787501586387', '9787501586448', '9787544258210')
list1 = []
for url in urls:
url0 = 'https://search.dangdang.com/?key={}'.format(url)
print(url0)
url = url0
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
}
# 获取响应
respond = requests.get(url, headers=headers)
# respond.encoding=''
print(respond.status_code)
html = respond.text
# with open('dang.html','w') as f:
# f.write(respond.text)02.
htmls = etree.HTML(html)
ul = htmls.xpath('//*[@id="search_nature_rg"]/ul/li[1]') # 获取书单
# print(ul)
for li in ul:
book_name = li.xpath("./p[1]/a[1]/@title") # 书名
# print(book_name)
# book_price = li.xpath('./p[3]/span[1]/text()') # 价格
# print(book_price)
author = li.xpath('./p[@class="search_book_author"]/span[1]//text()') # 作者
# print(author)
press = li.xpath("./p[@class='search_book_author']/span[3]/a/text()") # 出版社
# print(press)
comments = li.xpath("p[4]/a/text()") # 评论总数
# print(comments)
introduction = li.xpath("p[2]/text()") # 简介
# print(introduction)
print(book_name, author, press, comments, introduction)
# 保存到excel
book_info = xlwt.Workbook(encoding='utf-8') # 创建workbook对象
book_sheet = book_info.add_sheet('sheet1') # 通过矩阵坐标指定要输入位置,创建表
# 写入数据,第一行参数表示“行”,第二个参数“列”,第三个参数是内容
book_sheet.write(0, 0, '书名')
book_sheet.write(0, 1, '作者')
book_sheet.write(0, 2, '出版社')
book_sheet.write(0, 3, '评论总数')
book_sheet.write(0, 4, '简介')
#将爬取到的不同字段分别保存到对应格子
book_sheet.write(1, 0, book_name)
book_sheet.write(1, 1, author)
book_sheet.write(1, 2, press)
book_sheet.write(1, 3, comments)
book_sheet.write(1, 4, introduction)
book_info.save('book_info.xls') # 保存数据到名为book_info的excel文件里
求指点求指点😭