python爬虫采集文章基础信息保存excel案例

python爬虫采集文章基础信息保存excel案例

import requests
from bs4 import BeautifulSoup
import openpyxl

wb = openpyxl.Workbook() 
sheet = wb.active
sheet.title = '文章'
sheet['A1'] = '标题'
sheet['b1'] = '摘要'
sheet['c1'] = 'url'
sheet['d1'] = '发布时间'
sheet['e1'] = 'pic地址'

#引用requests库

for i in range(10):
    res = requests.get('https://www.jb51.net/html5/list551_'+ str(i) +'.html')

    res.encoding='gbk'
    #定义Reponse对象的编码为utf-8。
    html = res.text
    #把Response对象的内容以字符串的形式返回

    soup = BeautifulSoup(html,'html.parser') 

    items = soup.find_all(class_='item-inner')

    for item in items:
        title = item.find('p').text
        zhaiyao = item.find(class_='item-infode').text
        url = 'https://www.jb51.net'+item.find('p').find('a')['href']
        addtime = item.find(class_='lbtn').text

        try:
            picadress = item.find('img')['src']
        except:
            picadress = ''

        print('标题',title)
        print('摘要',zhaiyao)
        print('url',url)
        print('发布时间',addtime)
        print('图片地址',picadress)
        sheet.append([title,zhaiyao,url,addtime,picadress])
        print('----------------')

wb.save(r'c:/Users/Administrator/Desktop/文章.xlsx')
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值