爬取内容
import requests
from pyquery import PyQuery as pq
category = [] # 存储分类名称
tag = [] # 存储文章标签
title = [] # 存储文章标题
url = 'https://www.zhihu.com/explore' # 目标URL
headers = {
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36')
# 一行写不下的话可以使用括号() ,或者在第一行末尾加上' \'
}
html = requests.get(url, headers=headers).text
doc = pq(html)
# 根据属性名称获取对应的item
item1 = doc('.ExploreSpecialCard-info').items()
item2 = doc('.ExploreSpecialCard-contentTag').items()
item3 = doc('.ExploreSpecialCard-contentTitle').items()
for i in item1:
category.append(i.text())
for i in item2:
tag.append(i.text())
for i in item3:
title.append(i.text())
# 打印结果
print(category)
print(tag)
print(title)
显然,内容获取成功,但是排版不太美观,那就稍做处理,把item1的属性改一下,然后美化输出结果。
import requests
from pyquery import PyQuery as pq
category = [] # 存储分类名称
tag = [] # 存储文章标签
title = [] # 存储文章标题
url = 'https://www.zhihu.com/explore' # 目标URL
headers = {
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36')
# 一行写不下的话可以使用括号() ,或者在第一行末尾加上' \'
}
html = requests.get(url, headers=headers).text
doc = pq(html)
# 根据属性名称获取对应的item
item1 = doc('.ExploreSpecialCard-title').items()
item2 = doc('.ExploreSpecialCard-contentTag').items()
item3 = doc('.ExploreSpecialCard-contentTitle').items()
for i in item1:
category.append(i.text())
for i in item2:
tag.append(i.text())
for i in item3:
title.append(i.text())
# 打印结果
for i in range(len(category)):
print('专题分类:', category[i])
for j in range(3):
print('标签:{}'.format(tag[i*3+j]), end=' ---> ')
print('标题:{}'.format(title[i*3+j]))
print('=' * 30)
存储爬取的内容
txt文本存储
import requests
from pyquery import PyQuery as pq
category = [] # 存储分类名称
tag = [] # 存储文章标签
title = [] # 存储文章标题
url = 'https://www.zhihu.com/explore' # 目标URL
headers = {
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36')
# 一行写不下的话可以使用括号() ,或者在第一行末尾加上' \'
}
html = requests.get(url, headers=headers).text
doc = pq(html)
# 根据属性名称获取对应的item
item1 = doc('.ExploreSpecialCard-title').items()
item2 = doc('.ExploreSpecialCard-contentTag').items()
item3 = doc('.ExploreSpecialCard-contentTitle').items()
for i in item1:
category.append(i.text())
for i in item2:
tag.append(i.text())
for i in item3:
title.append(i.text())
# r:只读 w:覆盖写入 a:追加写入 b:二进制形式
file = open('explore.txt', 'w', encoding='utf-8')
# 打印结果
for i in range(len(category)):
file.write(''.join(category[i]))
file.write('\n')
for j in range(3):
file.write(''.join([tag[i*3+j]]))
file.write(' ---> ')
file.write(''.join([title[i*3+j]]))
file.write('\n')
file.write('\n' + '='*50 + '\n')
file.close()
json文件存储
json的字符串要用双引号包围,不能用单引号。
import json
data = [{
'name': '航少',
'gender': '男',
'age': 21
}]
with open('data.json', 'w', encoding='utf-8') as file:
file.write(json.dumps(data, indent=2, ensure_ascii=False))
# loads()与dumps()相对
# indent=2表示json数据缩进的字符个数为2
# encoding 加上 ensure_ascii 用于输出中文
csv文件存储
import requests
import csv
import codecs
from pyquery import PyQuery as pq
category = [] # 存储分类名称
tag = [] # 存储文章标签
title = [] # 存储文章标题
url = 'https://www.zhihu.com/explore' # 目标URL
headers = {
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36')
# 一行写不下的话可以使用括号() ,或者在第一行末尾加上' \'
}
html = requests.get(url, headers=headers).text
doc = pq(html)
# 根据属性名称获取对应的item
item1 = doc('.ExploreSpecialCard-title').items()
item2 = doc('.ExploreSpecialCard-contentTag').items()
item3 = doc('.ExploreSpecialCard-contentTitle').items()
for i in item1:
category.append(i.text())
for i in item2:
tag.append(i.text())
for i in item3:
title.append(i.text())
# r:只读 w:覆盖写入 a:追加写入 b:二进制形式
'''
python3中,write(codecs.BOM_UTF8)必须在以二进制打开文件时才可用
而二进制与我们拿到的数据格式(字符串)不符合
所以将文件分两次打开,第一次改形式防止乱码(用二进制的方式),第二次写数据(用非二进制方式)
'''
# 第一次打开时,解决中文乱码问题
file1 = open('explore.csv', 'wb')
file1.write(codecs.BOM_UTF8)
# 第二次打开时,再写入数据
file2 = open('explore.csv', 'w', newline='')
csv_file = csv.writer(file2)
# 写入数据
csv_file.writerow(['专题分类', '标签', '标题'])
for i in range(len(category)):
for j in range(3):
csv_file.writerow([category[i], tag[i*3+j], title[i*3+j]])
file1.close()
file2.close()
如果不分两次打开文件,就会总是报错。
数据库存储
import requests
import pymysql
from pyquery import PyQuery as pq
category = [] # 存储分类名称
tag = [] # 存储文章标签
title = [] # 存储文章标题
url = 'https://www.zhihu.com/explore' # 目标URL
headers = {
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36')
# 一行写不下的话可以使用括号() ,或者在第一行末尾加上' \'
}
html = requests.get(url, headers=headers).text
doc = pq(html)
# 根据属性名称获取对应的item
item1 = doc('.ExploreSpecialCard-title').items()
item2 = doc('.ExploreSpecialCard-contentTag').items()
item3 = doc('.ExploreSpecialCard-contentTitle').items()
for i in item1:
category.append(i.text())
for i in item2:
tag.append(i.text())
for i in item3:
title.append(i.text())
# 连接数据库
db = pymysql.connect(host='localhost', user='root', password='root', port=3306, db='test', charset='utf8')
cursor = db.cursor()
sql1 = 'create table explore (catagory varchar(256) not null, tag varchar(256) not null, title varchar(256) not null)'
sql2 = 'insert into explore (catagory, tag, title) values(%s, %s, %s)'
# 保持事务的一致性
try:
cursor.execute(sql1) # 创建数据表
for i in range(len(category)):
for j in range(3):
cursor.execute(sql2, (category[i], tag[i*3+j], title[i*3+j]))
db.commit()
except Exception as e:
print('报错信息:', e)
db.rollback()
db.close()
其他数据库存储
其他的数据库的操作和MySQL差不多,无非具体函数有所差别。