一、开始
首先呢,需要搭建一个 python 的编译环境,具体步骤请看:Sublime 使用笔记(一):安装 & 配置编译环境
二、安装工具包
需要用到 requests
json
openpyxl
这三个工具包。因为 python 自带了 json
工具包,所以只需要安装另外两个就好。步骤如下:
- 按 win键 + R,输入 cmd 后回车,打开命令提示符,并输入
pip install requests
。 - 待安装完成后,输入
pip list
,若在列表中看到了 requests ,则说明安装成功。 - 顺便补充一下,若在安装过程中提示 pip 版本可以进行更新,则可以输入
python -m pip install --upgrade pip
进行升级。 - 另一个工具包同理
三、开始写代码
import requests
URL = 'https://baidu.com'
resp = requests.get(URL)
print(resp.text)
解释一下,import
关键字用来引用所需的工具包。get
方法用于向网站所在的服务器发送请求,并且把返回的内容存入一个变量 resp
中。最有一句,print
是输出语句,.text
就是获取请求返回的文本。这样就可以输出我们获取到的网页了。
四、补充
在使用 requests.get()
来请求某些网页的时候,可能需要加上请求头,即把爬虫伪装成浏览器来获得访问权限。
import requests
URL = ' https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=43139143227&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45'}
resp = requests.get(URL, headers = header)
print(resp.text)
请求头添加方法为:
- 按 F12 打开调试,点击 “网络”,如果没有内容的话就刷新一下页面。
- 选中自己 URL 对应的请求,点击 “标头”。
- 找到
User-Agent
这一项,复制下来。 - 如上面代码所示添加到
requests.get()
中。
五、将爬取到的数据进行整理
我们需要使用 json.loads()
方法来整理数据,但是用改方法处理的数据必须以大括号开头、结尾,所以在这之前,我们需要用 .replace()
方法来进行预处理。
import requests
import json
URL = {'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=43139143227&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'}
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45'}
resp = requests.get(URL)
content = resp.text.replace('fetchJSON_comment98(', '').replace(');', '')
json_data = json.loads(content)
六、选取需要用到的数据
要理解这其中的原理,需要一些前端的知识,可以参考一下 《Dreamweaver CS6 完全自学教程》笔记 但是由于我学的也不深,所以仅作参考,仅作参考。
具体代码如下:
import requests
import json
URL = {'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=43139143227&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'}
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45'}
resp = requests.get(URL)
content = resp.text.replace('fetchJSON_comment98(', '').replace(');', '')
json_data = json.loads(content)
comments = json_data['comments']
for item in comments :
color = item['productColor']
size = item['productSize']
print(color)
print(size)
七、将数据放到 Excel 表格中
这一步要用到如下方法:
openpyxl.Workbook()
: 创建一个 Excel.create_sheet()
: 在 Excel 中创建一个工作表append([第一列, 第二列, ...])
: 将数据添加到 Excel 中.save('路径/文件名.xlsx')
将创建的 Excel 保存到一个 Excel 文件中
import requests
import json
import openpyxl
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45'}
URL = ' https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=43139143227&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'
wk = openpyxl.Workbook()
sheet = wk.create_sheet()
resp = requests.get(URL, headers = header)
content = resp.text.replace('fetchJSON_comment98(', '').replace(');', '')
json_data = json.loads(content)
comments = json_data['comments']
for item in comments:
color = item['productColor']
size = item['productSize']
sheet.append([color, size])
wk.save('py爬虫真好玩.xlsx')
八、碎碎念
本篇博客也算是浅尝辄止,虽说我知道 python 应用广泛、功能强大,我早晚会学它,但是由于个人规划及课程安排,在近期内可能不会在这方面深耕。由于各种机缘巧合,这几天简单的学习了一下 python 爬虫,也因此写下这篇博客,权当是为以后的学习做少许准备(希望会有帮助。对于文中的错误,也希望看到的各位能及时、直白的指出,谢谢~