亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

使用Python爬蟲(chóng)庫(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)開(kāi)之后,信息如下:

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

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

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

#-*-coding:utf-8-*-
import requests
from requests.exceptions import RequestException
 
#貓眼電影網(wǎng)站有反爬蟲(chó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)站有反爬蟲(chó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)開(kāi)每一頁(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)站有反爬蟲(chó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)站有反爬蟲(chó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爬蟲(chóng)庫(kù)requests多線程抓取貓眼電影TOP100數(shù)據(jù)的實(shí)例,更多關(guān)于Python爬蟲(chóng)庫(kù)的知識(shí)請(qǐng)查看下面的相關(guān)鏈接

相關(guān)文章

  • python http接口自動(dòng)化腳本詳解

    python http接口自動(dòng)化腳本詳解

    這篇文章主要為大家詳細(xì)介紹了python http接口自動(dòng)化腳本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 基于Python實(shí)現(xiàn)Excel轉(zhuǎn)Markdown表格

    基于Python實(shí)現(xiàn)Excel轉(zhuǎn)Markdown表格

    Markdown(也簡(jiǎn)稱md)作為一種輕量級(jí)標(biāo)記語(yǔ)言,因其易寫易讀,效果美觀大方,不僅被眾多網(wǎng)站使用,也是程序員們做筆記、寫文檔的首選。本文將利用Python實(shí)現(xiàn)Excel轉(zhuǎn)Markdown表格,感興趣的可以了解一下
    2022-04-04
  • 用Python展示動(dòng)態(tài)規(guī)則法用以解決重疊子問(wèn)題的示例

    用Python展示動(dòng)態(tài)規(guī)則法用以解決重疊子問(wèn)題的示例

    這篇文章主要介紹了用Python展示動(dòng)態(tài)規(guī)則法用以解決重疊子問(wèn)題的一個(gè)棋盤游戲的示例,動(dòng)態(tài)規(guī)劃常常適用于有重疊子問(wèn)題和最優(yōu)子結(jié)構(gòu)性質(zhì)的問(wèn)題,且耗時(shí)間往往遠(yuǎn)少于樸素解法,需要的朋友可以參考下
    2015-04-04
  • pandas中關(guān)于nan的處理方式

    pandas中關(guān)于nan的處理方式

    這篇文章主要介紹了pandas中關(guān)于nan的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python爬蟲(chóng)之爬取筆趣閣小說(shuō)

    python爬蟲(chóng)之爬取筆趣閣小說(shuō)

    這篇文章主要介紹了python爬蟲(chóng)之爬取筆趣閣小說(shuō),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python爬蟲(chóng)的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-04-04
  • 淺談Python中的異常和JSON讀寫數(shù)據(jù)的實(shí)現(xiàn)

    淺談Python中的異常和JSON讀寫數(shù)據(jù)的實(shí)現(xiàn)

    今天小編就為大家分享一篇淺談Python中的異常和JSON讀寫數(shù)據(jù)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • Django框架模板介紹

    Django框架模板介紹

    今天小編就為大家分享一篇關(guān)于Django框架模板介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Flask框架運(yùn)用WTForms實(shí)現(xiàn)用戶注冊(cè)的示例詳解

    Flask框架運(yùn)用WTForms實(shí)現(xiàn)用戶注冊(cè)的示例詳解

    WTForms 是用于web開(kāi)發(fā)的靈活的表單驗(yàn)證和呈現(xiàn)庫(kù),它可以與您選擇的任何web框架和模板引擎一起工作,并支持?jǐn)?shù)據(jù)驗(yàn)證、CSRF保護(hù)、國(guó)際化等。本文將運(yùn)用WTForms實(shí)現(xiàn)用戶注冊(cè)功能,需要的可以參考一下
    2022-12-12
  • Python 學(xué)習(xí)教程之networkx

    Python 學(xué)習(xí)教程之networkx

    networkx是Python的一個(gè)包,用于構(gòu)建和操作復(fù)雜的圖結(jié)構(gòu),提供分析圖的算法。對(duì)Python networkx相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧
    2019-04-04
  • Python數(shù)據(jù)容器dict(字典)的實(shí)現(xiàn)

    Python數(shù)據(jù)容器dict(字典)的實(shí)現(xiàn)

    本文主要介紹了Python數(shù)據(jù)容器dict(字典)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評(píng)論