Python实现爬虫抓取与读写、追加到excel文件操作示例
作者:masterbu 时间:2023-11-10 09:08:34
本文实例讲述了Python实现爬虫抓取与读写、追加到excel文件操作。分享给大家供大家参考,具体如下:
爬取糗事百科热门
安装 读写excel 依赖 pip install xlwt
安装 追加excel文件内容 依赖 pip install xlutils
安装 lxml
Python示例:
import csv
import requests
from lxml import etree
import time
import xlwt
import os
from xlutils.copy import copy
import xlrd
data_infos_list = []
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36'}
# f = open('C:\\Users\\Administrator\\Desktop\\qiubaibook.csv', 'a+', newline='', encoding='utf-8')
# writer = csv.writer(f)
# writer.writerow(('author', 'sex', 'rank', 'content', 'great', 'comment', 'time'))
filename = 'C:\\Users\\Administrator\\Desktop\\qiubaibook.xls'
def get_info(url):
res = requests.get(url, headers=headers)
selector = etree.HTML(res.text)
# print(res.text)
htmls = selector.xpath('//div[contains(@class,"article block untagged mb15")]')
# // *[ @ id = "qiushi_tag_120024357"] / a[1] / div / span 内容
# //*[@id="qiushi_tag_120024357"]/div[2]/span[1]/i 好笑
# //*[@id="c-120024357"]/i 评论
# //*[@id="qiushi_tag_120024357"]/div[1]/a[2]/h2 作者
# //*[@id="qiushi_tag_120024357"]/div[1]/div 等级
# // womenIcon manIcon 性别
for html in htmls:
author = html.xpath('div[1]/a[2]/h2/text()')
if len(author) == 0:
author = html.xpath('div[1]/span[2]/h2/text()')
rank = html.xpath('div[1]/div/text()')
sex = html.xpath('div[1]/div/@class')
if len(sex) == 0:
sex = '未知'
elif 'manIcon' in sex[0]:
sex = '男'
elif 'womenIcon' in sex[0]:
sex = '女'
if len(rank) == 0:
rank = '-1'
contents = html.xpath('a[1]/div/span/text()')
great = html.xpath('div[2]/span[1]/i/text()') # //*[@id="qiushi_tag_112746244"]/div[3]/span[1]/i
if len(great) == 0:
great = html.xpath('div[3]/span[1]/i/text()')
comment = html.xpath('div[2]/span[2]/a/i/text()') # //*[@id="c-112746244"]/i
if len(comment) == 0:
comment = html.xpath('div[3]/span[2]/a/i/text()')
# classes = html.xpath('a[1]/@class')
# writer.writerow((author[0].strip(), sex, rank[0].strip(), contents[0].strip(), great[0].strip(),
# comment[0].strip(), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))))
data_infos = [author[0].strip(), sex, rank[0].strip(), contents[0].strip(), great[0].strip(),
comment[0].strip(), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))]
data_infos_list.append(data_infos)
def write_data(sheet, row):
for data_infos in data_infos_list:
j = 0
for data in data_infos:
sheet.write(row, j, data)
j += 1
row += 1
if __name__ == '__main__':
urls = ['https://www.qiushibaike.com/8hr/page/{}/'.format(num) for num in range(1, 14)]
for url in urls:
print(url)
get_info(url)
time.sleep(2)
# 如果文件存在,则追加。如果文件不存在,则新建
if os.path.exists(filename):
# 打开excel
rb = xlrd.open_workbook(filename, formatting_info=True) # formatting_info=True 保留原有字体颜色等样式
# 用 xlrd 提供的方法获得现在已有的行数
rn = rb.sheets()[0].nrows
# 复制excel
wb = copy(rb)
# 从复制的excel文件中得到第一个sheet
sheet = wb.get_sheet(0)
# 向sheet中写入文件
write_data(sheet, rn)
# 删除原先的文件
os.remove(filename)
# 保存
wb.save(filename)
else:
header = ['author', 'sex', 'rank', 'content', 'great', 'comment', 'time']
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('糗百')
# 向 excel 中写入表头
for h in range(len(header)):
sheet.write(0, h, header[h])
# 向sheet中写入内容
write_data(sheet, 1)
book.save(filename)
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/lA6Nf/article/details/79352112
标签:Python,爬虫,excel
0
投稿
猜你喜欢
django项目搭建与Session使用详解
2021-05-19 11:37:31
MySQL 5.0.16乱码问题的解决方法
2024-01-23 07:07:27
将各种类型或对象都转变为数组
2009-12-28 13:19:00
python tkinter模块的简单使用
2021-11-20 16:52:33
解决Pycharm 运行后没有输出的问题
2023-06-02 04:48:19
Python读取HTML中的canvas并且以图片形式存入Word文档
2022-09-03 04:50:33
Flask实现swagger在线文档与接口测试流程详解
2023-04-29 11:34:17
在ASP中使用SQL语句之9:表单操作
2007-08-11 13:18:00
利用WSH获取计算机硬件信息、DNS信息等
2008-05-05 13:04:00
Linux下python与C++使用dlib实现人脸检测
2023-06-13 16:13:20
详解MySQL数据库--多表查询--内连接,外连接,子查询,相关子查询
2024-01-24 05:20:34
Python assert断言声明,遇到错误则立即返回问题
2022-05-10 13:38:50
js正则表达式验证密码强度【推荐】
2024-04-29 13:39:30
python RC4加密操作示例【测试可用】
2021-09-23 03:14:24
python正则表达式匹配不包含某几个字符的字符串方法
2023-06-11 16:52:45
python中的tcp示例详解
2021-06-21 21:09:46
Python编程之event对象的用法实例分析
2023-02-19 09:29:50
SQLServer触发器调用JavaWeb接口的过程详解
2024-01-17 12:08:45
CSS制作圆角导航的另一思路
2008-11-06 11:39:00
一文吃透Go的内置RPC原理
2024-02-03 08:45:53