爬取貓眼電影排行榜

2022-03-16 22:59:44 字數 1176 閱讀 9384

#

匯入我們需要的模組

import

reimport

requests

#一、獲取網頁內容

#(1)宣告目標url,就是爬取的**位址

base_url = "

"#(2)模仿瀏覽器

headers =

#(3)發起請求

response = requests.get(base_url, headers=headers)

#(4)接收響應的資料

html =response.text

#(5)將接收的資料寫入

with open("

maoyan.html

", '

w', encoding='

utf-8

') as f:

f.write(html)

#2.提取資料

#(1)縮小範圍(通過正則獲取資料)

pattern = re.compile(r'

.*?'

,re.s)

movie_list =pattern.findall(html)

#(2) 分別拿取每部電影中的資料

for movie in

movie_list:

#print(movie)

#獲取排名資訊

pattern = re.compile(r'

(\d)')

index =pattern.findall(movie)[0]

index = '

排名:' +index

print

(index)

#獲取電影名稱資訊

pattern = re.compile(r'

title="(.*?)"')

title =pattern.findall(movie)[0]

title = '

電影名稱:

' +title

print

(title)

#獲取資訊

pattern = re.compile(r'

maoyan.txt

", '

a+', encoding='

utf-8

') as f:

f.write(result)

scrapy爬取貓眼電影排行榜

做爬蟲的人,一定離不開的乙個框架就是scrapy框架,寫小專案的時候可以用requests模組就能得到結果,但是當爬取的資料量大的時候,就一定要用到框架.下面先練練手,用scrapy寫乙個爬取貓眼電影的程式,環境配置和scrapy安裝略過 第一步肯定是終端執行建立爬蟲專案和檔案 1 建立爬蟲專案 2...

爬取豆瓣電影推薦排行榜

import requests from bs4 import beautifulsoup class dianying def html url self,url html requests.get url soup beautifulsoup html.text,lxml pai soup.se...

爬取貓眼電影排行100電影

import json import requests from requests.exceptions import requestexception import re import time 獲取單頁的內容 def get one page url try response requests....