天天看點

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')