python爬取足球直播吧五大联赛积分榜

yipeiwu_com6年前Python爬虫

本文实例为大家分享了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设计】。

相关文章

Python爬虫爬取新浪微博内容示例【基于代理IP】

Python爬虫爬取新浪微博内容示例【基于代理IP】

本文实例讲述了Python爬虫爬取新浪微博内容。分享给大家供大家参考,具体如下: 用Python编写爬虫,爬取微博大V的微博内容,本文以女神的微博为例(爬新浪m站:https://m.w...

Python实现抓取城市的PM2.5浓度和排名

Python实现抓取城市的PM2.5浓度和排名

主机环境:(Python2.7.9 / Win8_64 / bs4) 利用BeautifulSoup4来抓取 www.pm25.com 上的PM2.5数据,之所以抓取这个网站,是因为上面...

利用Python3分析sitemap.xml并抓取导出全站链接详解

利用Python3分析sitemap.xml并抓取导出全站链接详解

前言 最近网站从HTTPS转为HTTP,更换了网址,旧网址做了301重定向,折腾有点大,于是在百度站长平台提交网址,不管是主动推送还是手动提交,前提都是要整理网站的链接,手动添加太麻烦,...

Python实现爬虫抓取与读写、追加到excel文件操作示例

本文实例讲述了Python实现爬虫抓取与读写、追加到excel文件操作。分享给大家供大家参考,具体如下: 爬取糗事百科热门 安装 读写excel 依赖 pip install xlwt...

python 爬取马蜂窝景点翻页文字评论的实现

python 爬取马蜂窝景点翻页文字评论的实现

使用Chrome、python3.7、requests库和VSCode进行爬取马蜂窝黄鹤楼的文字评论(http://www.mafengwo.cn/poi/5426285.html)。...