爬去金10网数据,并写入到Excel表格里面(re,requests,xlwt)

import requests
import re
import xlwt

def Get_news():
    url = 'https://www.jin10.com/'
    html = requests.get(url)
    html.encoding = html.apparent_encoding
    reg = re.compile('<div class="jin-flash_time">(.*?)</div></div><div class="jin-flash_b"><h4>(.*?)</h4></div></div>')
    flash_news = re.findall(reg,html.text)
    return flash_news

    
def excel_write(flash_news):
    newtable = 'jin10.xls'          #创建一个Excel文件名称
    wb = xlwt.Workbook(encoding = 'utf-8')          #创建Excel文件
    ws = wb.add_sheet('jin_new')            #创建一个Excel工作表格
    headDate = ['时间','新闻内容']        #创建标题头
    for colnum in range(0,2):
        ws.write(0,colnum,headDate[colnum])         
    
    index = 1           #表示从第二行开始
    for flash_new in flash_news:
        for i in range(0,2):
            #print(flash_new[i])
            ws.write(index,i,flash_new[i])
        index += 1
        wb.save(newtable)
        
excel_write(Get_news())
View Code

还不是很完美,新闻的内容里面是有<b></b>标签,也还有空白的单元格没有处理,最关键的一点是没有做到实时监控

转载于:https://www.cnblogs.com/114811yayi/p/6762741.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值