金狮镖局 Design By www.egabc.com
使用Python爬虫库requests多线程抓取猫眼电影TOP100思路:
- 查看网页源代码
- 抓取单页内容
- 正则表达式提取信息
- 猫眼TOP100所有信息写入文件
- 多线程抓取
- 运行平台:windows
- Python版本:Python 3.7.
- IDE:Sublime Text
- 浏览器:Chrome浏览器
1.查看猫眼电影TOP100网页原代码
按F12查看网页源代码发现每一个电影的信息都在“<dd></dd>”标签之中。
点开之后,信息如下:
2.抓取单页内容
在浏览器中打开猫眼电影网站,点击“榜单”,再点击“TOP100榜”如下图:
接下来通过以下代码获取网页源代码:
#-*-coding:utf-8-*- import requests from requests.exceptions import RequestException #猫眼电影网站有反爬虫措施,设置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' } #爬取网页源代码 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()
执行结果如下:
3.正则表达式提取信息
上图标示信息即为要提取的信息,代码实现如下:
#-*-coding:utf-8-*- import requests import re from requests.exceptions import RequestException #猫眼电影网站有反爬虫措施,设置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' } #爬取网页源代码 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 parse_one_page(html): pattern = re.compile('<dd>.*"(.*".*"><a' +'.*?>(.*">(.*">(.*">(.*">(.*"https://maoyan.com/board/4" html = get_one_page(url,headers) for item in parse_one_page(html): print(item) if __name__ == '__main__': main()
执行结果如下:
4.猫眼TOP100所有信息写入文件
上边代码实现单页的信息抓取,要想爬取100个电影的信息,先观察每一页url的变化,点开每一页我们会发现url进行变化,原url后面多了‘?offset=0',且offset的值变化从0,10,20,变化如下:
代码实现如下:
#-*-coding:utf-8-*- import requests import re import json import os from requests.exceptions import RequestException #猫眼电影网站有反爬虫措施,设置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' } #爬取网页源代码 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 parse_one_page(html): pattern = re.compile('<dd>.*"(.*".*"><a' +'.*?>(.*">(.*">(.*">(.*">(.*"https://maoyan.com/board/4"+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__': #对每一页信息进行爬取 for i in range(10): main(i*10)
爬取结果如下:
5.多线程抓取
进行比较,发现多线程爬取时间明显较快:
多线程:
以下为完整代码:
#-*-coding:utf-8-*- import requests import re import json import os from requests.exceptions import RequestException from multiprocessing import Pool #猫眼电影网站有反爬虫措施,设置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' } #爬取网页源代码 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 parse_one_page(html): pattern = re.compile('<dd>.*"(.*".*"><a' +'.*?>(.*">(.*">(.*">(.*">(.*"https://maoyan.com/board/4"+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__': #对每一页信息进行爬取 pool = Pool() pool.map(main,[i*10 for i in range(10)]) pool.close() pool.join()
本文主要讲解了使用Python爬虫库requests多线程抓取猫眼电影TOP100数据的实例,更多关于Python爬虫库的知识请查看下面的相关链接
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无python爬虫开发之使用Python爬虫库requests多线程抓取猫眼电影TOP100实例的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 归龙潮三枚天灯碎片位置在哪 笼律鸟任务天灯图文位置
- 归龙潮新手开荒阵容怎么搭配 新手前期阵容养成攻略
- 归龙潮中浦城区福鼠位置在哪 中浦城区福鼠收集位置大全
- MatteoPasqualini-JohannSebastianBachItalienischesKonzertFranzsischeOuvertureVierDue
- 【雨果唱片】陈百强《梦里人LPCD45》2012[WAV+CUE]
- 王菲.1994-《迷路》美卡引进版[WAV+CUE]
- 老外遇到八戒裸体Bug 网友:我需要用它对付蜘蛛精
- 《黑神话》超强越化补丁:游戏里有文字的地都改了
- 玩家吐槽《黑神话:悟空》唐僧相貌:一脸衰样 没半点气质
- 群星.1994-烧得厉害VOL.2我不是一个人拙滚石】【WAV+CUE】
- 群星.1994-烧得厉害VOL.3风笑痴【滚石】【WAV+CUE】
- 群星.1995-烧得厉害VOL.4两个世界【滚石】【WAV+CUE】
- 归龙潮中浦城区宝箱位置收集大全 中浦区宝箱解密攻略
- 黑神话悟空上品幽灯鬼精魄获取方法一览|上品幽灯鬼精魄收集攻略
- 《电锯糖心:RePOP》发售首日表现亮眼,玩家峰值超越《星鸣特攻》