import requests,json,csv
from lxml import etree
# url = '/match/team_players.htm?divisionId=874902863023837184&teamId=892361870411960321'
# res = requests.get(url).text
# print(res)
# select = etree.HTML(res)
# name = select.xpath('//span[@class="player-name-value player-short-words"]/text()')
# print(name)
# url = "/match/team_players_json.htm?divisionId=874902863023837184&teamId=892361870411960321&page=2"
# res = requests.post(url).text
# data = json.loads(res)['data']
# count = data['count']
# print(count)
def post_team_name(url):
try:
res = requests.post(url.format(1)).text
data = json.loads(res)['data']
count = data['count']
# print(count % 10)
#如果最后一頁不為0赘那,整除后需要加1刑桑,在加1是為了for的時候最后
pages = count // 10 + 2 if count % 10 > 0 else count // 10 + 1
# print(pages)
for page in range(1,pages):
page_res = requests.post(url.format(page)).text
page_data = json.loads(page_res)['data']
for list in page_data['list']:
teamName = list['teamName']
teamId = list['teamId']
teamPic = list['teamPic']
# print(teamName,teamId,teamPic)
#因為網(wǎng)頁有json數(shù)據(jù)還有網(wǎng)頁的可以用xpath來解析網(wǎng)頁
#使用json解析
# post_players_information(teamId,teamName)
#使用xpath解析
post_xpath_players_information(teamId,teamName)
except Exception as e:
print("post_team_name函數(shù)解析錯誤 錯誤為:",e)
#使用Json解析獲得數(shù)據(jù)
def post_players_information(teamId,teamName):
try:
play_url = "/match/team_players_json.htm?divisionId=874902863023837184&teamId={}".format(
teamId)
res = requests.post(play_url).text
play_data = json.loads(res)['data']
count = play_data['count']
# print(count)
pages = count // 10 + 2 if count % 10 > 0 else count // 10 + 1
# print(pages)
for page in range(1,pages):
url = "/match/team_players_json.htm?divisionId=874902863023837184&teamId={}&page={}".format(
teamId,page)
res = requests.post(url).text
play_data = json.loads(res)['data']
for list in play_data['list']:
print(teamName, teamId, count,list['nickname'],list['clothNo'],list['area'],list['age'],list['height'],list['weight'])
writer.writerow((teamName, teamId, count,list['nickname'],list['clothNo'],list['area'],list['age'],list['height'],list['weight']))
except Exception as e:
print("post_players_information函數(shù)解析錯誤 錯誤為:",e)
#使用xpath解析獲得數(shù)據(jù)
def post_xpath_players_information(teamId,teamName):
try:
play_url = "/match/team_players_json.htm?divisionId=874902863023837184&teamId={}".format(
teamId)
res = requests.post(play_url).text
play_data = json.loads(res)['data']
count = play_data['count']
# print(count)
pages = count // 10 + 2 if count % 10 > 0 else count // 10 + 1
# print(pages)
for page in range(1, pages):
url = "/match/team_players.htm?divisionId=874902863023837184&teamId={}&page={}".format(teamId,page)
res = requests.post(url).text
select = etree.HTML(res)
data = select.xpath('//div[@class="player-detail"]')
# 名字
names = select.xpath('//span[@class="player-name-value player-short-words"]/text()')
# 球衣號
nums = select.xpath('//span[@class="player-num-value"]/text()')
# 地區(qū)
addresss = select.xpath('//span[@class="player-address-value player-short-words"]/text()')
# 年齡
ages = select.xpath('//span[@class="player-age-value"]/text()')
# 身高
heights = select.xpath('//span[@class="player-height-value"]/text()')
# 體重
weights = select.xpath('//span[@class="player-weight-value"]/text()')
for i in range(0,len(nums)):
name = names[i]
num = nums[i]
address = addresss[i]
age = ages[i]
height = heights[i]
weight = weights[i]
print(teamName, teamId, count, name, num, address, age, height, weight)
writer.writerow((teamName, teamId, count, name, num, address, age, height, weight))
except Exception as e:
print("post_xpath_players_information函數(shù)解析錯誤 錯誤為:", e)
#隊伍對陣信息
def post_team_game(url):
try:
res = requests.post(url).text
select = etree.HTML(res)
game_times = select.xpath('//div[@class="event-time"]/span[1]/text()')
game_sorts = select.xpath('//div[@class="event-time"]/span[2]/text()')
team_one_names = select.xpath('//div[@class="team-one"]/span[@class="team-name"]/text()')
team_one_scores = select.xpath('//div[@class="team-one"]/span[@class="team-score"]/text()')
team_two_names = select.xpath('//div[@class="team-two"]/span[@class="team-name"]/text()')
team_two_scores = select.xpath('//div[@class="team-two"]/span[@class="team-score"]/text()')
for i in range(0,len(game_times)):
game_time = game_times[i]
game_sort = game_sorts[i]
team_one_name = team_one_names[i]
team_one_score = team_one_scores[i]
team_two_name = team_two_names[i]
team_two_score = team_two_scores[i]
print('{} {}, {} {}分 對陣 {} {}分'.format(game_time,game_sort,team_one_name,team_one_score,team_two_name,team_two_score))
except Exception as e:
print("post_team_game函數(shù)解析錯誤 錯誤為:", e)
if __name__ == "__main__":
f = open('chengshichuanqi.csv', 'w+', encoding='utf-8')
writer = csv.writer(f)
writer.writerow(('戰(zhàn)隊名稱', '戰(zhàn)隊id', '隊員數(shù)', '隊員名', '球衣號', '隊員地區(qū)','隊員年齡','隊員身高','隊員體重'))
base_url = '/match/join_teams_json.htm?divisionId=874902863023837184&page={}'
game_url = '/match/success_events.htm?divisionId=874902863023837184'
post_team_game(game_url)
post_team_name(base_url)
屏幕快照 2017-08-08 下午12.57.16.png
屏幕快照 2017-08-08 下午8.27.07.png