Python teaches you to collect best-selling book information data and save csv~

foreword

Hello! Hello everyone, this is the Demon King~**

[Environmental introduction]:

  • python 3.8 interpreter
  • pycharm 2021 Professional Edition >>> Activation Code Editor

[Module usage]:

  • requests >>> pip install requests
  • parsel >>> pip install parsel
  • csv

win + R Enter cmd Enter the installation command pip install module name


Module installation problem:

- 如果安装python第三方模块:
    1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
    2. 在pycharm中点击Terminal(终端) 输入安装命令
- 安装失败原因:
    - 失败一: pip 不是内部命令
        解决方法: 设置环境变量

    - 失败二: 出现大量报红 (read time out)
        解决方法: 因为是网络链接超时,  需要切换镜像源
            清华:https://pypi.tuna.tsinghua.edu.cn/simple
            阿里云:https://mirrors.aliyun.com/pypi/simple/
            中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
            华中理工大学:https://pypi.hustunique.com/
            山东理工大学:https://pypi.sdutlinux.org/
            豆瓣:https://pypi.douban.com/simple/
            例如:pip3 install -i https://pypi.doubanio.com/simple/ 模块名

    - 失败三: cmd里面显示已经安装过了, 或者安装成功了, 但是在pycharm里面还是无法导入
        解决方法: 可能安装了多个python版本 (anaconda 或者 python 安装一个即可) 卸载一个就好
                或者你pycharm里面python解释器没有设置好

How to configure the python interpreter in pycharm?

1. 选择file(文件) >>> setting(设置) >>> Project(项目) >>> python interpreter(python解释器)
2. 点击齿轮, 选择add
3. 添加python安装路径

How does pycharm install plugins?

1. 选择file(文件) >>> setting(设置) >>> Plugins(插件)
2. 点击 Marketplace  输入想要安装的插件名字 比如:翻译插件 输入 translation / 汉化插件 输入 Chinese
3. 选择相应的插件点击 install(安装) 即可
4. 安装成功之后 是会弹出 重启pycharm的选项 点击确定, 重启即可生效

Answers, information, source code click to receive~

code

import os
path = 'C:\\02-讲师文件夹\\自游\\虎牙视频\\video\\'
files = os.listdir(path)
print(files)
for file in files[:5]:
    name = file.split('.')[0]
    f = open(path + file, mode='rb')
    with open('转换\\' + name + '.rmvb', mode='wb') as z:
        z.write(f.read())

# os.listdir('')
# # 导入数据请求模
# 块
# import requests  # 第三方模块 pip install requests
# # 导入数据解析模块
# import parsel  # 第三方模块 pip install parsel
# import csv
# import time
# f = open('data_1.csv', mode='a', encoding='utf-8', newline='')
# csv_writer = csv.DictWriter(f, fieldnames=[
#     '标题',
#     '评论',
#     '推荐',
#     '作者',
#     '出版日期',
#     '出版社',
#     '售价',
#     '折扣',
#     '原价',
#     '电子书',
#     '详情页',
# ])
# csv_writer.writeheader() # 写入表头
# """
# 1. 发送请求
# 爬虫发送请求,  模拟浏览器对于url地址发送请求的
# headers 请求头
# """
# for page in range(1, 26):
#     print(f'正在采集第{page}页的数据内容')
#     time.sleep(1.5)
#     url = f'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-recent7-0-0-1-{page}'
#     headers = {
    
    
#         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36'
#     }
#     response = requests.get(url=url, headers=headers)
#     # print(response)  # <Response [200]> 响应对象 200 状态码表示请求成功
#     # 2. 获取数据, 服务器返回响应数据 response.text 文本数据
#     # print(response.text)
#     # 3. 解析数据, 提取我们想要数据内容 书籍相关信息 re css xpath
#     # css 根据标签属性内容提取数据
#     selector = parsel.Selector(response.text)  # 把获取html字符串转成可解析的对象
#     # 分为两次提取, 第一次提取所有li标签的内容
#     lis = selector.css('ul.bang_list li')
#     # print(lis)
#     for li in lis:
#         # class 用小圆点 id 用 #  标签名
#         # a::attr(title) 提取a标签里面title属性内容 attr() 属性选择器 get() 获取这个数据内容
#         title = li.css('.name a::attr(title)').get()  # 书籍名字
#         comment = li.css('.star a::text').get().replace('条评论', '')  # 评论数
#         recommend = li.css('.tuijian::text').get().replace('推荐', '')  # 推荐
#         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_n = li.css('div:nth-child(7) p:nth-child(1) .price_n::text').get()  # 售价
#         price_r = li.css('div:nth-child(7) p:nth-child(1) .price_r::text').get()  # 原价
#         price_s = li.css('div:nth-child(7) p:nth-child(1) .price_s::text').get().replace('折', '')  # 折扣
#         price_e = li.css('div:nth-child(7) .price_e .price_n::text').get()  # 电子书价格
#         href = li.css('.name a::attr(href)').get()  # 详情页
#         dit = {
    
    
#             '标题': title,
#             '评论': comment,
#             '推荐': recommend,
#             '作者': author,
#             '出版日期': date,
#             '出版社': press,
#             '售价': price_n,
#             '折扣': price_s,
#             '原价': price_r,
#             '电子书': price_e,
#             '详情页': href,
#         }
#         csv_writer.writerow(dit)
#         print(title, comment, recommend, author, date, press, price_n, price_r, price_s, price_e, href)

video tutorial

Python crawls book bestseller list book information data, saves csv and displays data visualization

epilogue

Well, this article of mine ends here!

If you have more suggestions or questions, feel free to comment or private message me! Let's work hard together (ง •_•)ง

Follow the blogger if you like it, or like and comment on my article! ! !

Guess you like

Origin blog.csdn.net/python56123/article/details/124214008