新手爬取豆瓣读书250练手
本帖最后由 carole1102 于 2019-11-30 22:39 编辑在52学习python,刚接触爬虫,练手爬取,各位大佬轻拍。。。。
from lxml import etree
import requests
import csv
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
df = open(r'e:\douban.csv','wt',newline='',encoding='utf-8-sig')
writer = csv.writer(df)
writer.writerow(('name','url','author','publisher','date','price','rate','comment'))
urls = ['https://book.douban.com/top250?start={}'.format(str(i)) for i in range(0,250,25)]for url in urls: html = requests.get(url,headers=headers)
selector = etree.HTML(html.text)
infos = selector.xpath('//tr[@Class ="item"]')
for info in infos:
name = info.xpath('td/div/a/@title')
url = info.xpath('td/div/a/@href')
book_info = info.xpath('td/p/text()')
author = book_info.split('/')
publisher = book_info.split('/')
date = book_info.split('/')[-2]
price = book_info.split('/')[-1]
rate = info.xpath('td/div/span/text()')
comments = info.xpath('td/p/span/text()')
comment = comments if len(comments) != 0 else '空'
writer.writerow((name,url,author,publisher,date,price,rate,comment))
df.close() functionlike 发表于 2019-11-29 11:15
怎么执行,我准备复制你的代码来执行不了,新手
看报错吧 是不是依赖包 格式问题 ymhld 发表于 2019-11-29 19:09
刚开始学,还没上手,能爬指定的书吗
指定的书也可以 找到对应的详细div就行 有教程嘛 可以可以,学习学习 可以的。 怎么执行,我准备复制你的代码来执行不了,新手 感谢代码分享{:301_993:} 很不错,学习一下 感谢分享代码,试试 刚开始学,还没上手,能爬指定的书吗 感谢楼主分享
页:
[1]
2