最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python爬蟲開發(fā)之使用Python爬蟲庫(kù)requests多線程抓取貓眼電影TOP100實(shí)例

 更新時(shí)間:2020年03月10日 15:02:11   作者:real向往  
這篇文章主要介紹了python爬蟲開發(fā)之使用Python爬蟲庫(kù)requests多線程抓取貓眼電影TOP100實(shí)例,需要的朋友可以參考下

使用Python爬蟲庫(kù)requests多線程抓取貓眼電影TOP100思路:

  1. 查看網(wǎng)頁(yè)源代碼
  2. 抓取單頁(yè)內(nèi)容
  3. 正則表達(dá)式提取信息
  4. 貓眼TOP100所有信息寫入文件
  5. 多線程抓取
  • 運(yùn)行平臺(tái):windows
  • Python版本:Python 3.7.
  • IDE:Sublime Text
  • 瀏覽器:Chrome瀏覽器

1.查看貓眼電影TOP100網(wǎng)頁(yè)原代碼

按F12查看網(wǎng)頁(yè)源代碼發(fā)現(xiàn)每一個(gè)電影的信息都在“<dd></dd>”標(biāo)簽之中。

點(diǎn)開之后,信息如下:

2.抓取單頁(yè)內(nèi)容

在瀏覽器中打開貓眼電影網(wǎng)站,點(diǎn)擊“榜單”,再點(diǎn)擊“TOP100榜”如下圖:

接下來通過以下代碼獲取網(wǎng)頁(yè)源代碼:

#-*-coding:utf-8-*-
import requests
from requests.exceptions import RequestException
 
#貓眼電影網(wǎng)站有反爬蟲措施,設(shè)置headers后可以爬取
headers = {
	'Content-Type': 'text/plain; charset=UTF-8',
	'Origin':'https://maoyan.com',
	'Referer':'https://maoyan.com/board/4',
	'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
	}
 
#爬取網(wǎng)頁(yè)源代碼
def get_one_page(url,headers):
	try:
		response =requests.get(url,headers =headers)
		if response.status_code == 200:
			return response.text
		return None
	except RequestsException:
		return None
 
def main():
	url = "https://maoyan.com/board/4"
	html = get_one_page(url,headers)
	print(html)
 
if __name__ == '__main__':
	main()

執(zhí)行結(jié)果如下:

3.正則表達(dá)式提取信息

上圖標(biāo)示信息即為要提取的信息,代碼實(shí)現(xiàn)如下:

#-*-coding:utf-8-*-
import requests
import re
from requests.exceptions import RequestException
 
#貓眼電影網(wǎng)站有反爬蟲措施,設(shè)置headers后可以爬取
headers = {
	'Content-Type': 'text/plain; charset=UTF-8',
	'Origin':'https://maoyan.com',
	'Referer':'https://maoyan.com/board/4',
	'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
	}
 
#爬取網(wǎng)頁(yè)源代碼
def get_one_page(url,headers):
	try:
		response =requests.get(url,headers =headers)
		if response.status_code == 200:
			return response.text
		return None
	except RequestsException:
		return None
 
#正則表達(dá)式提取信息
def parse_one_page(html):
	pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
		+'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
	items = re.findall(pattern,html)
	for item in items:
		yield{
		'index':item[0],
		'image':item[1],
		'title':item[2],
		'actor':item[3].strip()[3:],
		'time':item[4].strip()[5:],
		'score':item[5]+item[6]
		}
 
def main():
	url = "https://maoyan.com/board/4"
	html = get_one_page(url,headers)
	for item in parse_one_page(html):
		print(item)
 
if __name__ == '__main__':
	main()

執(zhí)行結(jié)果如下:

4.貓眼TOP100所有信息寫入文件

上邊代碼實(shí)現(xiàn)單頁(yè)的信息抓取,要想爬取100個(gè)電影的信息,先觀察每一頁(yè)url的變化,點(diǎn)開每一頁(yè)我們會(huì)發(fā)現(xiàn)url進(jìn)行變化,原url后面多了‘?offset=0',且offset的值變化從0,10,20,變化如下:

代碼實(shí)現(xiàn)如下:

#-*-coding:utf-8-*-
import requests
import re
import json
import os
from requests.exceptions import RequestException
 
#貓眼電影網(wǎng)站有反爬蟲措施,設(shè)置headers后可以爬取
headers = {
	'Content-Type': 'text/plain; charset=UTF-8',
	'Origin':'https://maoyan.com',
	'Referer':'https://maoyan.com/board/4',
	'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
	}
 
#爬取網(wǎng)頁(yè)源代碼
def get_one_page(url,headers):
	try:
		response =requests.get(url,headers =headers)
		if response.status_code == 200:
			return response.text
		return None
	except RequestsException:
		return None
 
#正則表達(dá)式提取信息
def parse_one_page(html):
	pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
		+'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
	items = re.findall(pattern,html)
	for item in items:
		yield{
		'index':item[0],
		'image':item[1],
		'title':item[2],
		'actor':item[3].strip()[3:],
		'time':item[4].strip()[5:],
		'score':item[5]+item[6]
		}
#貓眼TOP100所有信息寫入文件
def write_to_file(content):
	#encoding ='utf-8',ensure_ascii =False,使寫入文件的代碼顯示為中文
	with open('result.txt','a',encoding ='utf-8') as f:
		f.write(json.dumps(content,ensure_ascii =False)+'\n')
		f.close()
#下載電影封面
def save_image_file(url,path):
 
	jd = requests.get(url)
	if jd.status_code == 200:
		with open(path,'wb') as f:
			f.write(jd.content)
			f.close()
 
def main(offset):
	url = "https://maoyan.com/board/4?offset="+str(offset)
	html = get_one_page(url,headers)
	if not os.path.exists('covers'):
		os.mkdir('covers')	
	for item in parse_one_page(html):
		print(item)
		write_to_file(item)
		save_image_file(item['image'],'covers/'+item['title']+'.jpg')
 
if __name__ == '__main__':
	#對(duì)每一頁(yè)信息進(jìn)行爬取
	for i in range(10):
		main(i*10)

爬取結(jié)果如下:

5.多線程抓取

進(jìn)行比較,發(fā)現(xiàn)多線程爬取時(shí)間明顯較快:

多線程:

以下為完整代碼:

#-*-coding:utf-8-*-
import requests
import re
import json
import os
from requests.exceptions import RequestException
from multiprocessing import Pool
#貓眼電影網(wǎng)站有反爬蟲措施,設(shè)置headers后可以爬取
headers = {
	'Content-Type': 'text/plain; charset=UTF-8',
	'Origin':'https://maoyan.com',
	'Referer':'https://maoyan.com/board/4',
	'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
	}
 
#爬取網(wǎng)頁(yè)源代碼
def get_one_page(url,headers):
	try:
		response =requests.get(url,headers =headers)
		if response.status_code == 200:
			return response.text
		return None
	except RequestsException:
		return None
 
#正則表達(dá)式提取信息
def parse_one_page(html):
	pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
		+'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
	items = re.findall(pattern,html)
	for item in items:
		yield{
		'index':item[0],
		'image':item[1],
		'title':item[2],
		'actor':item[3].strip()[3:],
		'time':item[4].strip()[5:],
		'score':item[5]+item[6]
		}
#貓眼TOP100所有信息寫入文件
def write_to_file(content):
	#encoding ='utf-8',ensure_ascii =False,使寫入文件的代碼顯示為中文
	with open('result.txt','a',encoding ='utf-8') as f:
		f.write(json.dumps(content,ensure_ascii =False)+'\n')
		f.close()
#下載電影封面
def save_image_file(url,path):
 
	jd = requests.get(url)
	if jd.status_code == 200:
		with open(path,'wb') as f:
			f.write(jd.content)
			f.close()
 
def main(offset):
	url = "https://maoyan.com/board/4?offset="+str(offset)
	html = get_one_page(url,headers)
	if not os.path.exists('covers'):
		os.mkdir('covers')	
	for item in parse_one_page(html):
		print(item)
		write_to_file(item)
		save_image_file(item['image'],'covers/'+item['title']+'.jpg')
 
if __name__ == '__main__':
	#對(duì)每一頁(yè)信息進(jìn)行爬取
	pool = Pool()
	pool.map(main,[i*10 for i in range(10)])
	pool.close()
	pool.join()

本文主要講解了使用Python爬蟲庫(kù)requests多線程抓取貓眼電影TOP100數(shù)據(jù)的實(shí)例,更多關(guān)于Python爬蟲庫(kù)的知識(shí)請(qǐng)查看下面的相關(guān)鏈接

相關(guān)文章

  • 基于python純函數(shù)實(shí)現(xiàn)井字棋游戲

    基于python純函數(shù)實(shí)現(xiàn)井字棋游戲

    這篇文章主要介紹了基于python純函數(shù)實(shí)現(xiàn)井字棋游戲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Flask??response?對(duì)象詳情

    Flask??response?對(duì)象詳情

    在?Flask?中,響應(yīng)使用?Response?對(duì)象表示,響應(yīng)報(bào)文中的大部分內(nèi)容由服務(wù)器處理,一般情況下,我們只負(fù)責(zé)返回主體內(nèi)容即可。在之前的文章中,我們了解到?Flask?會(huì)先匹配請(qǐng)求?url?的路由,調(diào)用對(duì)應(yīng)的視圖函數(shù),視圖函數(shù)的返回值構(gòu)成了響應(yīng)報(bào)文的主體內(nèi)容。
    2021-11-11
  • python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件

    python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件

    這篇文章主要介紹了python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 淺談編碼,解碼,亂碼的問題

    淺談編碼,解碼,亂碼的問題

    下面小編就為大家?guī)硪黄獪\談編碼,解碼,亂碼的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • 分享python數(shù)據(jù)統(tǒng)計(jì)的一些小技巧

    分享python數(shù)據(jù)統(tǒng)計(jì)的一些小技巧

    今天這些小技巧在處理python的一些數(shù)據(jù)方面還是很有幫助的,希望能幫到在這方面有需要的童鞋~
    2016-07-07
  • anaconda虛擬環(huán)境默認(rèn)路徑的更改圖文教程

    anaconda虛擬環(huán)境默認(rèn)路徑的更改圖文教程

    在Anaconda中如果沒有指定路徑,虛擬環(huán)境會(huì)默認(rèn)安裝在anaconda所安裝的目錄下,這篇文章主要給大家介紹了關(guān)于anaconda虛擬環(huán)境默認(rèn)路徑更改的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • 詳解Python直接賦值,深拷貝和淺拷貝

    詳解Python直接賦值,深拷貝和淺拷貝

    這篇文章主要介紹了Python直接賦值,深拷貝和淺拷貝的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Python assert語(yǔ)句的簡(jiǎn)單使用示例

    Python assert語(yǔ)句的簡(jiǎn)單使用示例

    這篇文章主要給大家介紹了關(guān)于Python assert語(yǔ)句的簡(jiǎn)單使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python isalnum()函數(shù)的具體使用

    Python isalnum()函數(shù)的具體使用

    本文主要介紹了Python isalnum()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 超詳細(xì)注釋之OpenCV旋轉(zhuǎn)圖像任意角度

    超詳細(xì)注釋之OpenCV旋轉(zhuǎn)圖像任意角度

    這篇文章主要介紹了OpenCV旋轉(zhuǎn)圖像任意角度,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

佛坪县| 塔河县| 河东区| 柳河县| 乌拉特前旗| 朔州市| 镇原县| 卓资县| 固始县| 永泰县| 兴和县| 汝城县| 北宁市| 鞍山市| 射阳县| 东兴市| 桦南县| 沁水县| 中超| 张家川| 丹江口市| 原平市| 祥云县| 金阳县| 沈丘县| 托克托县| 张北县| 通榆县| 岫岩| 大方县| 闸北区| 天气| 阳信县| 五莲县| 岳普湖县| 平江县| 齐河县| 静安区| 利津县| 红河县| 乌鲁木齐市|