python爬蟲入門 豆瓣電影排行榜top250

2021-09-25 03:22:09 字數 857 閱讀 6649

1.requests

2.re(正規表示式庫)

請求頭:

此處複製的火狐瀏覽器請求頭

myheader =
標記電影次序:time,初始化為1

排行榜第i頁:link = '' + str(i * 25)

正則匹配結果:matchobj

import requests

import re

def get_movies():

#請求頭

myheader =

time = 1

for i in range(0, 10):

#迴圈訪問http

link = '' + str(i * 25)

r = requests.get(link, headers=myheader, timeout=10)

#輸出頁面狀態碼

print(str(i+1), "code:", r.status_code)

#正則匹配

matchobj = re.findall(r'(?<=)[^&]*(?=)', r.text)

#寫入檔案

with open("res.txt", "a", encoding='utf-8') as f:

for num in matchobj:

f.write(str(time) + ':' + num + '\n')

time += 1

#函式呼叫

get_movies()

python爬蟲 豆瓣電影

最近學習python 順便寫下爬蟲練手 爬的是豆瓣電影排行榜 python版本2.7.6 安裝 beautiful soup sudo apt get install python bs4 安裝 requests sudo apt get install python requests下面是py a...

python爬蟲之獲取豆瓣電影資訊

本質就是 發起請求 獲取響應內容 解析內容 儲存資料首先,需要做的就是匯入模組pip install requests pip install lxml coding utf 8 import requests from lxml import etree 選取網頁並做解析 這裡以 titanic ...

Python爬蟲 爬取豆瓣電影(二)

檢視上乙個專案,請看 上乙個專案中獲取到了一定數量的電影url資訊,這次來獲取單個電影的電影詳情。對傳遞的url返回乙個名為soup的beautifulsoup物件 defget url html soup url header request body.get header proxies req...