当前位置:   article > 正文

Python爬虫入门教程27:爬取某电商平台数据内容并做数据可视化_python爬取电商数据

python爬取电商数据

response = requests.get(url=url, headers=headers)

print(response.text)

selector = parsel.Selector(response.text)

lis = selector.css(‘.bang_list_mode li’)

for li in lis:

title = li.css(‘.name a::attr(title)’).get() # 标题/书名

recommend = li.css(‘.tuijian::text’).get() # 推荐

star = li.css(‘.star a::text’).get() # 评价

author = li.css(‘div:nth-child(5) a:nth-child(1)::attr(title)’).get() # 作者

date = li.css(‘div:nth-child(6) span::text’).get() # 出版日期

press = li.css(‘div:nth-child(6) a::text’).get() # 出版社

price_r = li.css(‘.price .price_r::text’).get() # 原价

price_n = li.css(‘.price .price_n::text’).get() # 售价

price_e = li.css(‘.price_e span::text’).get() # 电子书价格

href = li.css(‘.name a::attr(href)’).get() # 详情页

dit = {

‘标题’: title,

‘推荐’: recommend,

‘评价’: star,

‘作者’: author,

‘出版日期’: date,

‘出版社’: press,

‘原价’: price_r,

‘售价’: price_n,

‘电子书价格’: price_e,

‘详情页’: href,

}

with open(‘书籍.csv’, mode=‘a’, encoding=‘utf-8’) as f:

f.write(f’{title},{recommend},{star},{author},{date},{press},{price_r},{price_n},{price_e}\n’)

csv_writer.writerow(dit)

print(title, recommend, star, author, date, press, price_r, price_n, price_e, sep=’ | ')

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/940381
推荐阅读
相关标签