3

Python 爬虫爬取当当网

 1 year ago
source link: https://blog.51cto.com/u_15668438/5581371
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

 一、模块使用

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

win + R 输入cmd 输入安装命令 pip install 模块名 (如果你觉得安装速度比较慢, 你可以切换国内镜像源)

二、模块安装问题

    - 如果安装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解释器没有设置好

三、如何配置pycharm里面的python解释器

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

四、pycharm如何安装插件

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

五、爬虫基本流程

(1). 数据来源分析 <重要...>
    1. 要分析自己想要数据内容, 可以请求那个url地址得到相应数据
        开发者 不会2  会用1
        1. F12打开开发者工具, 刷新网页
        2. 通过关键字搜索, 找寻数据包

(2). 代码实现步骤过程
    1. 发送请求, 模拟浏览器对于url地址发送get请求
    2. 获取数据, 获取服务器返回响应数据 ---> 开发者工具里面response
    3. 解析数据, 提取我们想要数据内容
    4. 保存数据, 把数据保存csv表格

六、完整代码

# 导入数据请求模块 ---> 需要 pip install requests
import requests
# 导入数据解析模块 ---> 需要 pip install parsel
import parsel
# 导入csv模块
import csv
# 导入时间模块
import time
# 创建文件 data.csv 文件名 mode='a' 保存方式 encoding='utf-8' 编码格式 newline='' 新队列
f = open('data.csv', mode='a', encoding='utf-8', newline='')
# 字典写入 f 文件对象 fieldnames 字段名 表格第一行数据 表头
csv_writer = csv.DictWriter(f, fieldnames=[
'书名',
'评论',
'推荐',
'作者',
'时间',
'出版社',
'售价',
'原价',
'折扣',
'电子书',
'详情页',
])
# 写入表头
csv_writer.writeheader()
"""
发送请求, 模拟浏览器对于url地址发送get请求
为什么要模拟浏览器?
为了防止被反爬
"""
for page in range(1, 26):
print(f'正在采集第{page}页的数据内容')
# 确定网址
url = f'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-recent30-0-0-1-{page}'
# 请求头 ---> 字典数据类型, 构建完整键值对
headers = {
# User-Agent: 用户代理 表示浏览器基本身份标识
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
# 发送请求
response = requests.get(url=url, headers=headers)
# <Response [200]> 响应对象 200 状态码 表示请求成功
print(response)
# 获取数据, 获取服务器返回响应数据 ---> 开发者工具里面response response.text ---> 文本数据 字符串数据
# print(response.text)
# 解析数据 parsel(解析模块) --> css选择器 xpath 转换解析对象 <Selector xpath=None data='<html xmlns="http://www.w3.org/1999/x...'>
selector = parsel.Selector(response.text)
"""
css选择器 ---> 通过标签属性内容提取数据 开发者工具里面 元素面板 去查看标签
所有数据信息, 都包含li标签里面

"""
# 获取所有li标签 --> 返回列表, 列表里面元素都是 Selector对象
lis = selector.css('.bang_list_mode li')
# for循环遍历 可以把列表里面元素一个一个提取 ---> 相当于在一个箱子里面, 把苹果一个一个拿出来
for li in lis:
# title是a标签里面什么东西? 属性 ---> attr() 提取标签里属性
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('.publisher_info .a::attr(title)').get() # 作者
date = li.css('.publisher_info span::text').get() # 时间
press = li.css('div:nth-child(6) a::text').get() # 出版社
price_n = li.css('.price .price_n::text').get() # 售价
price_r = li.css('.price .price_r::text').get() # 原价
price_s = li.css('.price .price_s::text').get().replace('折', '') # 折扣
price_e = li.css('.price .price_e .price_n::text').get() # 电子书
href = li.css('.name a::attr(href)').get() # 详情页
dit = {
'书名': title,
'评论': comment,
'推荐': recommend,
'作者': author,
'时间': date,
'出版社': press,
'售价': price_n,
'原价': price_r,
'折扣': price_s,
'电子书': price_e,
'详情页': href,
}
# 快速复制一行 ctrl + D
csv_writer.writerow(dit)
print(title, comment, recommend, author, date, press, price_n, price_r, price_s, price_e, href)
Python 爬虫爬取当当网_数据_02

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK