Python爬蟲例項(二)爬取資料後併入excel

2021-10-01 03:37:11 字數 1008 閱讀 6018

報錯:module not found error: no module named 'openpyxl'

原始碼:

import requests

from lxml import etree

import pandas as pd

#設定請求頭

headers =

cookies =

#儲存列表

film =

#封裝url請求方法

def get_film(url,headers,cookies):

# 傳送get請求

response = requests.get(url, cookies=cookies, headers=headers).text

# 獲取解析結果,etrrr.html只能解析str

tree = etree.html(response)

name_list = tree.xpath('.//div[@class="hd"]/a/span[@class="title"][1]/text()')

film.extend(name_list)

if __name__ == '__main__':

for value in list(range(0, 250, 25)):

# 生成每一頁的位址

url = "" + str(value) + "&filter="

get_film(url, headers, cookies)

#使用pandas模組將資料儲存到excel檔案中

data1 = pd.dataframe()

#將資料寫入到excel**中

data1.to_excel(excel_writer="豆瓣電影top250.xlsx",sheet_name='電影排行',columns=['電影名稱'])

print(film)

Python爬蟲例項,爬取小說

import pprint import requests from bs4 import beautifulsoup 獲取原始碼 defget source url r requests.get url if r.status code 200 print r.status code 錯誤 rai...

python爬蟲例項 爬取歌單

學習自 從零開始學python 爬取酷狗歌單,儲存入csv檔案 直接上源 含注釋 import requests 用於請求網頁獲取網頁資料 from bs4 import beautifulsoup 解析網頁資料 import time time庫中的sleep 方法可以讓程式暫停 import c...

Python 爬蟲例項(4) 爬取網易新聞

自己閒來無聊,就爬取了網易資訊,重點是分析網頁,使用抓包工具詳細的分析網頁的每個鏈結,資料儲存在sqllite中,這裡只是簡單的解析了新聞頁面的文字資訊,並未對資訊進行解析 僅供參考,不足之處請指正 coding utf 8 import random,re import sqlite3 impor...