您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關python如何爬取足球直播吧五大聯賽積分榜的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
使用BeautifulSoup4解析爬取足球直播吧五大聯賽積分榜信息;
#! /usr/bin/python3 # -*- coding:utf-8 -*- from urllib.request import urlopen from urllib.request import quote from bs4 import BeautifulSoup import time import re import json import pymysql url="https://data.zhibo8.cc/pc_main_data/#/dejia/index1" response = urlopen(url) bs = BeautifulSoup(response,"html.parser") # print(bs) liansai_s = bs.select("ul.data_nav > li") # print(liansai_lists) # <li ng-class="{current:current==0}" ng-click="selcuptype(0,'NBA')"> # <a data-index="nba" href="#/team_nba/nba" rel="external nofollow" name="NBA"> # <div class="data_item" ng-class="{current2:current==0}">NBA</div> # <div class="data_item_logo"><img src="images/nba.png"/></div> # </a> # </li> liansai_lists = [] for liansai in liansai_s: # print(liansai) ls = [] href = liansai.select("a")[0].get("href") # print(href) lsName = liansai.select("a > div.data_item")[0].text ls.append(href) ls.append(lsName) liansai_lists.append(ls) # print(liansai_lists) #{ # '排名': '1', # 'teamId': '565', # '球隊': '巴塞羅那', # '場次': '29', # '勝': '23', # '平': '6', # '負': '0', # '進/失球': '74/13', # '凈勝球': '61', # '積分': '75', # '球隊圖標': 'http://duihui.qiumibao.com/zuqiu/basailuona.png', # '字體顏色': '#e62e2e', # '夜間字體顏色': '#af2d2d', # '背景顏色': '#ffffff', # '球隊名稱': '巴塞羅那' # }, location = '' def insert_into_db(lists): #將數據寫入到數據庫中 con = pymysql.connect(host="localhost", user="root", password="root", database="zhi_bo_ba", charset='utf8', port=3306) # 游標。 作用 就等同于 JDBC 中的 Statement cursor = con.cursor() for record in lists: print(record) sql_insert = "insert into jifenbang (liansai, paiming, qiudui, changci, sheng,ping,fu,jinqiu_shiqu,jingsheng,jifen) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" cursor.execute(sql_insert, record) con.commit() cursor.close() con.close() def jifenbang(str): # print(str) datas = urlopen(str) #加載json類型的數據 datas = json.load(datas) jifen_list = [] for info in datas['data']: j_f = [] j_f.append(location) j_f.append(info['排名']) j_f.append(info['球隊']) j_f.append(info['場次']) j_f.append(info['勝']) j_f.append(info['平']) j_f.append(info['負']) j_f.append(info['進/失球']) j_f.append(info['凈勝球']) j_f.append(info['積分']) jifen_list.append(j_f) print(jifen_list) insert_into_db(jifen_list) # https://dc.qiumibao.com/shuju/public/index.php?_url=/data/index&league=聯賽&tab=%積分榜&year=[year] # https://dc.qiumibao.com/shuju/public/index.php?_url=/data/index&league=聯賽&tab=球員榜&type=射手榜&year=[year] urls="https://dc.qiumibao.com/shuju/public/index.php?_url=/data/index&league=" list = ['西甲','英超','意甲','德甲','法甲','中超','中甲'] for aa in liansai_lists: if aa[1] in list: location = aa[1] new_url = '%s%s&tab=%s&year=[year]' % (urls,quote(aa[1]),quote('積分榜')) jifenbang(new_url)
感謝各位的閱讀!關于“python如何爬取足球直播吧五大聯賽積分榜”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。