当前位置 博文首页 > LY的博客:多线程爬取中超全部2018赛季职业球员
首先:
继续爬虫球探网,该网使用局部加载,真实信息在隐藏URL ,要得到全部球员信息,首先要得到中超职业联赛全部球队的id,
?
?
原始链接:http://zq.win007.com/cn/TeamHeadPage/2018/60.html
找到请求链接为:http://zq.win007.com/jsData/teamInfo/team60.js?version=20190127180910
var arrLeague = [60,'中国超级联赛','中國超級聯賽','Chinese Super League','ThisYear','#0066FF','league_match/images/20140111105821.jpg','中超','中超','CHA CSL']; var arrTeam = [[39,'上海绿地申花','上海绿地申花','Shanghai Shenhua','上海申花','images/20160202133545.png'],[40,'山东鲁能泰山','山東魯能泰山','Shandong Luneng','山東魯能泰山','images/20180921192230.png'],[43,'北京中赫国安','北京中赫國安','Beijing Guoan','北京國安','images/2013322171729.png'],[83,'重庆斯威','重慶斯威','Chongqing SWM Motors','重慶斯威','images/20180921192706.png'],[89,'天津泰达亿利','天津泰達','Tianjin Teda','天津泰達','images/20180921183229.png'],[528,'长春亚泰','長春亞泰','Changchun Yatai','長春亞泰','images/20180921180113.png'],[535,'广州恒大淘宝','廣州恆大淘寶','Guangzhou Evergrande Taobao FC','廣州恆大淘寶','images/20160302150130.jpg'],[541,'河南建业','河南建業','Henan Jianye','河南建業','images/20180921200639.png'],[543,'江苏苏宁','江蘇蘇寧','Jiangsu Suning FC','江蘇舜天','images/20180921195311.png'],[3969,'北京人和','北京人和','Beijing Renhe','貴州茅臺','images/20180921194257.png'],[7642,'上海上港','上海上港','Shanghai East Asia FC','上海上港','images/20180921174420.png'],[8685,'天津天海','天津天海','Tianjin QuanJian','天津松江','images/20180921181058.png'],[8686,'贵州恒丰智诚','貴州恒丰智誠','Guizhou Hengfeng Zhicheng','貴州智誠','images/20180921193147.png'],[13969,'大连一方','大連一方','Dalian Aerbin','大連阿爾濱','images/20180921195501.png'],[16668,'广州富力','廣州富力','Guangzhou Fuli FC','廣州富力','images/20140211195608.jpg'],[16961,'河北华夏幸福','河北華夏幸福','Hebei HX Xingfu','河北華夏幸福','images/20180921193444.png']]; var lastUpdateTime = '2019-01-27 13:37:17';
简单变化一下
其实只是要拿到teamid
[39, 40, 43, 83, 89, 528, 535, 541, 543, 3969, 7642, 8685, 8686, 13969, 16668, 16961]
?
再套用我前一篇文章 的爬虫框架
python BS4 收集上港队球员赛季表现??https://blog.csdn.net/qq_37195257/article/details/86665367
# coding=gbk from bs4 import BeautifulSoup import requests import pandas as pd from concurrent.futures import ThreadPoolExecutor import time start = time.clock() teamid=[39, 40, 43, 83, 89, 528, 535, 541, 543, 3969, 7642, 8685, 8686, 13969, 16668, 16961] name=[] nation=[] age=[] goal=[] yellow=[] red=[] df= pd.DataFrame({'nation':nation,'goal':goal,'yellow':yellow,'red':red},index=name) def getpage(x): global df url='http://zq.win007.com/cn/team/PlayerDataAjax.aspx?SclassID=60&matchSeason=2018&teamID=' header={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/...... Safari/537.36'} url=str(url)+str(x) name=[] nation=[] age=[] goal=[] yellow=[] red=[] try: html=requests.get(url,headers=header).text #print (html) soup=BeautifulSoup(html,'lxml') #用BeautifulSoup来解析获取的子页面html代码 #获取球员名 for k in soup.find_all('div',align="left"): k=k.get_text() k=k.replace('\n','') k=k.replace(' ','') print (k) name.append(k) print (soup.find_all('div',align="center")) print (len(soup.find_all('div',align="center"))) print(soup.find_all('div',align="center")[9:]) for k in soup.find_all('div',align="center")[9:]: k=k.get_text() k=k.replace('\n','') k=k.replace(' ','') print (k) la=[] #获取球员属性 for k in soup.find_all('div',align="center")[9:]: k=k.get_text() k=k.replace('\n','') k=k.replace(' ','') print (k) la.append(k) print (len(la)) print ('#############') print([la[i] for i in range(0, len(la), 8)]) print([la[i] for i in range(0+1, len(la)+1, 8)]) nation=[la[i] for i in range(0+1, len(la)+1, 8)] print([la[i] for i in range(0+2, len(la)+2, 8)]) goal=[la[i] for i in range(0+2, len(la)+2, 8)] print([la[i] for i in range(0+4, len(la)+4, 8)]) red=[la[i] for i in range(0+5, len(la)+5, 8)] print([la[i] for i in range(0+5, len(la)+5, 8)]) yellow=[la[i] for i in range(0+6, len(la)+6, 8)] print([la[i] for i in range(0+6, len(la)+6, 8)]) print ('#############') df2= pd.DataFrame({'nation':nation,'goal':goal,'yellow':yellow,'red':red},index=name) print (df2) df=pd.concat([df,df2]) teamid.remove(x) except: pass # 多线程 def multithreading(): sum=0 while len(teamid)>0: with ThreadPoolExecutor(max_workers=4) as executor: for result in executor.map(getpage, teamid): sum+=1 return sum def main(): sum=multithreading() print (teamid) print (df) end = time.clock() # 计时-结束 print (("爬取完成 用时:")) print ((end - start)) print ('总爬取 %d 只球队 '%(sum)) if __name__=='__main__': main()
?
结果:
#############
? name? ? ? ? goal nation red yellow
塞德里克.巴坎布 ? ?19 ?刚果民主共和国 ? 2 ? ? ?6
J.韦拉 ? ? ? ?11 ? ? ?西班牙 ? 0 ? ? ?4
雷纳托.奥古斯托 ? ?10 ? ? ? 巴西 ? 0 ? ? ?2
若纳坦.索里亚诺 ? ? 9 ? ? ?西班牙 ? 0 ? ? ?0
张稀哲 ? ? ? ? ?4 ? ? ? 中国 ? 1 ? ? ?2
朴成 ? ? ? ? ? 3 ? ? ? 中国 ? 0 ? ? ?3
于大宝 ? ? ? ? ?2 ? ? ? 中国 ? 0 ? ? ?3
韦世豪 ? ? ? ? ?2 ? ? ? 中国 ? 0 ? ? ?4
于洋 ? ? ? ? ? 1 ? ? ? 中国 ? 0 ? ? ?3
池忠国 ? ? ? ? ?1 ? ? ? 中国 ? 0 ? ? ?2
雷腾龙 ? ? ? ? ?1 ? ? ? 中国 ? 0 ? ? ?1
巴顿 ? ? ? ? ? 1 ? ? ? 中国 ? 0 ? ? ?0
吕鹏 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?1
侯森 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?2
晋鹏翔 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?2
刘欢 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?1
金泰延 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?1
张瑀 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?4
胡延强 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?2
姜涛 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?6
塔尔德利 ? ? ? ?17 ? ? ? 巴西 ? 0 ? ? ?4
格拉吉亚诺.佩莱利 ? 16 ? ? ?意大利 ? 5 ? ? ?1
金敬道 ? ? ? ? ?6 ? ? ? 中国 ? 0 ? ? ?4
吴兴涵 ? ? ? ? ?4 ? ? ? 中国 ? 1 ? ? ?4
吉尔 ? ? ? ? ? 3 ? ? ? 巴西 ? 0 ? ? ?2
蒿俊闵 ? ? ? ? ?2 ? ? ? 中国 ? 0 ? ? ?0
古迪斯 ? ? ? ? ?2 ? ? ? 巴西 ? 0 ? ? ?1
刘军帅 ? ? ? ? ?2 ? ? ? 中国 ? 0 ? ? ?6
刘洋 ? ? ? ? ? 1 ? ? ? 中国 ? 0 ? ? ?1
张驰 ? ? ? ? ? 1 ? ? ? 中国 ? 1 ? ? ?9
... ? ? ? ?... ? ? ?... ?.. ? ?...
马俊亮 ? ? ? ? ?1 ? ? ? 中国 ? 0 ? ? ?1
李提香 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?4
姜积弘 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?4
程月磊 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?2
张功 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?4
丁海峰 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?3
弋腾 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?3
唐淼 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?8
张辰龙 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?2
黄政宇 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?2
埃泽奎尔·拉维奇 ? ?12 ? ? ?阿根廷 ? 2 ? ? ?2
董学升 ? ? ? ? 12 ? ? ? 中国 ? 1 ? ? ?1
A.卡比 ? ? ? ? 5 ? ? ?摩洛哥 ? 2 ? ? ?0
张呈栋 ? ? ? ? ?4 ? ? ? 中国 ? 1 ? ? ?5
安德森·埃尔纳尼斯 ? ?3 ? ? ? 巴西 ? 1 ? ? ?1
高华泽 ? ? ? ? ?2 ? ? ? 中国 ? 1 ? ? ?0
姜至鹏 ? ? ? ? ?1 ? ? ? 中国 ? 1 ? ? ?1
任航 ? ? ? ? ? 1 ? ? ? 中国 ? 0 ? ? ?5
宋文杰 ? ? ? ? ?1 ? ? ? 中国 ? 1 ? ? ?1
金洋洋 ? ? ? ? ?1 ? ? ? 中国 ? 0 ? ? ?2
罗森文 ? ? ? ? ?1 ? ? ? 中国 ? 0 ? ? ?3
桂宏 ? ? ? ? ? 1 ? ? ? 中国 ? 0 ? ? ?0
哈维尔·马斯切拉诺 ? ?0 ? ? ?阿根廷 ? 0 ? ? 10
库阿西·热尔维尼奥 ? ?0 ? ? 科特迪瓦 ? 0 ? ? ?1
赵明剑 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?6
杜文洋 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?3
王秋明 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?4
赵宇豪 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?2
高準翼 ? ? ? ? ?0 ? ? ? 中国 ? 0 ? ? ?2
杨程 ? ? ? ? ? 0 ? ? ? 中国 ? 0 ? ? ?1
[336 rows x 4 columns]
?
cs