[Python] 【爬虫】python爬取MSDN站所有P2P下载链接
作者:精品下载站 日期:2020-03-27 00:00:00 浏览:76 分类:编程开发
今日,msdn的新网站开放注册,然后体验了一波,发现要强制观看30S的广告才可以下载,因此就想提前把资源爬取下来以便后用。
先来看下成果:
1,网站分析
1.1通过直接爬取:https://msdn.itellyou.cn/,可以获得8个ID,对应着侧边栏的八个分类
1.2没展开一个分类,会发送一个POST请求
传递的就是之前获取的8个ID之一
1.3查看这个请求的返回值,可以看到又获得一个ID,以及对应的资源名称。
1.4点击,展开一个资源可以发现,又多了两个POST请求
1.4.1第一个GETLang,经分析大概意思就是,获取资源的语言,然后这个请求也发送了一个ID,然后在返回值中又获得一个ID,这就是后文中的lang值
1.4.2第二个GetList,这个传递了三个参数:
(1)ID:经对比可发现这个ID就是我们之前一直在用的ID。
(2)lang,我后来才发现是language的缩写,就是语言的意思,我们从第一个GetLang的返回值可以获取,这个lang值。
(3)filter,翻译成中文就是过滤器的意思,对应图片坐下角的红色框框内是否勾选。
1.4.3到这里就以及在返回值中获得了下载地址了:
综上就是分析过程。然后就开始敲代码了
2,为了追求速度,选择了Scrapy框架。然后代码自己看吧。
爬虫.py:
#-*-coding:utf-8-*- importjson importscrapy frommsdn.itemsimportMsdnItem classMsdndownSpider(scrapy.Spider): name='msdndown' allowed_domains=['msdn.itellyou.cn'] start_urls=['http://msdn.itellyou.cn/'] defparse(self,response): self.index=[iforiinresponse.xpath('//h4[@class="panel-title"][email protected]').extract()] #self.index_title=[iforiinresponse.xpath('//h4[@class="panel-title"]/a/text()').extract()] url='https://msdn.itellyou.cn/Category/Index' foriinself.index: yieldscrapy.FormRequest(url=url,formdata={'id':i},dont_filter=True, callback=self.Get_Lang,meta={'id':i}) defGet_Lang(self,response): id_info=json.loads(response.text) url='https://msdn.itellyou.cn/Category/GetLang' foriinid_info:#遍历软件列表 lang=i['id']#软件ID title=i['name']#软件名 #进行下一次爬取,根据lang(语言)id获取软件语言ID列表 yieldscrapy.FormRequest(url=url,formdata={'id':lang},dont_filter=True,callback=self.Get_List, meta={'id':lang,'title':title}) defGet_List(self,response): lang=json.loads(response.text)['result'] id=response.meta['id'] title=response.meta['title'] url='https://msdn.itellyou.cn/Category/GetList' #如果语言为空则跳过,否则进行下次爬取下载地址 iflen(lang)!=0: #遍历语言列表ID foriinlang: data={ 'id':id, 'lang':i['id'], 'filter':'true' } yieldscrapy.FormRequest(url=url,formdata=data,dont_filter=True,callback=self.Get_Down, meta={'name':title,'lang':i['lang']}) else: pass defGet_Down(self,response): response_json=json.loads(response.text)['result'] item=MsdnItem() foriinresponse_json: item['name']=i['name'] item['url']=i['url'] print(i['name']+"--------------"+i['url'])#测试输出,为了运行时不太无聊 returnitem
items.py:
#-*-coding:utf-8-*- #Defineherethemodelsforyourscrapeditems # #Seedocumentationin: #https://docs.scrapy.org/en/latest/topics/items.html importscrapy classMsdnItem(scrapy.Item): #definethefieldsforyouritemherelike: name=scrapy.Field() url=scrapy.Field()
settings.py:
#-*-coding:utf-8-*- #Scrapysettingsformsdnproject # #Forsimplicity,thisfilecontainsonlysettingsconsideredimportantor #commonlyused.Youcanfindmoresettingsconsultingthedocumentation: # #https://docs.scrapy.org/en/latest/topics/settings.html #https://docs.scrapy.org/en/latest/topics/downloader-middleware.html #https://docs.scrapy.org/en/latest/topics/spider-middleware.html BOT_NAME='msdn' SPIDER_MODULES=['msdn.spiders'] NEWSPIDER_MODULE='msdn.spiders' #Crawlresponsiblybyidentifyingyourself(andyourwebsite)ontheuser-agent #USER_AGENT='msdn(+http://www.yourdomain.com)' #Obeyrobots.txtrules ROBOTSTXT_OBEY=False #ConfiguremaximumconcurrentrequestsperformedbyScrapy(default:16) #CONCURRENT_REQUESTS=32 #Configureadelayforrequestsforthesamewebsite(default:0) #Seehttps://docs.scrapy.org/en/latest/topics/settings.html#download-delay #Seealsoautothrottlesettingsanddocs DOWNLOAD_DELAY=0.1 #Thedownloaddelaysettingwillhonoronlyoneof: #CONCURRENT_REQUESTS_PER_DOMAIN=16 #CONCURRENT_REQUESTS_PER_IP=16 #Disablecookies(enabledbydefault) #COOKIES_ENABLED=False #DisableTelnetConsole(enabledbydefault) #TELNETCONSOLE_ENABLED=False #Overridethedefaultrequestheaders: DEFAULT_REQUEST_HEADERS={ 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language':'en', 'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/80.0.3987.149Safari/537.36' } #Enableordisablespidermiddlewares #Seehttps://docs.scrapy.org/en/latest/topics/spider-middleware.html #SPIDER_MIDDLEWARES={ #'msdn.middlewares.MsdnSpiderMiddleware':543, #} #Enableordisabledownloadermiddlewares #Seehttps://docs.scrapy.org/en/latest/topics/downloader-middleware.html #DOWNLOADER_MIDDLEWARES={ #'msdn.middlewares.MsdnDownloaderMiddleware':543, #} #Enableordisableextensions #Seehttps://docs.scrapy.org/en/latest/topics/extensions.html #EXTENSIONS={ #'scrapy.extensions.telnet.TelnetConsole':None, #} #Configureitempipelines #Seehttps://docs.scrapy.org/en/latest/topics/item-pipeline.html ITEM_PIPELINES={ 'msdn.pipelines.MsdnPipeline':300, } #EnableandconfiguretheAutoThrottleextension(disabledbydefault) #Seehttps://docs.scrapy.org/en/latest/topics/autothrottle.html #AUTOTHROTTLE_ENABLED=True #Theinitialdownloaddelay #AUTOTHROTTLE_START_DELAY=5 #Themaximumdownloaddelaytobesetincaseofhighlatencies #AUTOTHROTTLE_MAX_DELAY=60 #TheaveragenumberofrequestsScrapyshouldbesendinginparallelto #eachremoteserver #AUTOTHROTTLE_TARGET_CONCURRENCY=1.0 #Enableshowingthrottlingstatsforeveryresponsereceived: #AUTOTHROTTLE_DEBUG=False #EnableandconfigureHTTPcaching(disabledbydefault) #Seehttps://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings #HTTPCACHE_ENABLED=True #HTTPCACHE_EXPIRATION_SECS=0 #HTTPCACHE_DIR='httpcache' #HTTPCACHE_IGNORE_HTTP_CODES=[] #HTTPCACHE_STORAGE='scrapy.extensions.httpcache.FilesystemCacheStorage'
pipelines.py:
#-*-coding:utf-8-*- #Defineyouritempipelineshere # #Don'tforgettoaddyourpipelinetotheITEM_PIPELINESsetting #See:https://docs.scrapy.org/en/latest/topics/item-pipeline.html classMsdnPipeline(object): def__init__(self): self.file=open('msdnc.csv','a+',encoding='utf8') defprocess_item(self,item,spider): title=item['name'] url=item['url'] self.file.write(title+'*'+url+' ') defdown_item(self,item,spider): self.file.close()
main.py(启动文件):
fromscrapy.cmdlineimportexecute execute(['scrapy','crawl','msdndown'])
3,成品打包地址点击进入:
猜你还喜欢
- 03-29 [编程相关] Winform窗体圆角以及描边完美解决方案
- 03-29 [前端问题] has been blocked by CORS policy跨域问题解决
- 03-29 [编程相关] GitHub Actions 入门教程
- 03-29 [编程探讨] CSS Grid 网格布局教程
- 10-12 [编程相关] python实现文件夹所有文件编码从GBK转为UTF8
- 10-11 [编程算法] opencv之霍夫变换:圆
- 10-11 [编程算法] OpenCV Camshift算法+目标跟踪源码
- 10-11 [Python] python 创建 Telnet 客户端
- 10-11 [编程相关] Python 基于 Yolov8 + CPU 实现物体检测
- 03-15 [脚本工具] 使用go语言开发自动化脚本 - 一键定场、抢购、预约、捡漏
- 01-08 [编程技术] 秒杀面试官系列 - Redis zset底层是怎么实现的
- 01-05 [编程技术] 《Redis设计与实现》pdf
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[短剧合集] 2025年06月2日 精选+付费短剧推荐39部
[软件合集] 25年6月2日 精选软件18个
[软件合集] 25年6月1日 精选软件15个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag