在《用Python模拟2018世界杯夺冠之路》一文中,我选择从公开的足球网站用爬虫抓取数据,从而建模并模拟比赛,但是略过了爬虫的实施细节。虽然爬虫并不难做,但希望可以让更多感兴趣的朋友自己动手抓数据下来玩,提供便利,今天就把我抓取球探网的方法和Python源码拿出来分享给大家,不超过100行代码。希望球友们能快速get爬虫的技能。
#-*- coding: utf-8 -*-
from __future__ import print_function, division
from selenium import webdriver
import pandas as pd
class Spider(object):
def __init__(self):
## setup
#self.base_url = base_url
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.verificationErrors = []
self.accept_next_alert = True
def get_all_team_data(self):
# 先通过世界杯主页获取所有32只队的ID(构成球队URL)
self.get_team_ids()
# 循环爬取每一支队的比赛数据
data = []
for i, [team_id, team_name] in enumerate(self.team_list):
#if i == 1:
# break
print(i, team_id, team_na