python中Biwenger的Html请求

2024-10-02 10:18:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用一个html请求从Biwenger中获取数据,但是响应返回的数据与在chrome中打开url时不同。你知道吗

这是我的密码

import requests

shots_url = "https://biwenger.as.com/user/naranjas-4537694"

response = requests.get(shots_url)
response.raise_for_status() # raise exception if invalid response

print(response.text)

我没有收到任何错误(),但是数据请求显示的数据与url和此消息中的数据不同:

<!doctype html><meta charset=utf-8><title>Biwenger</title><base href=/ ><meta...<div class=body><p>Looks like the browser you're using is not compatible with Biwenger :(<p>We recommend using <a href=http://www.google.com/chrome/ target=_blank>Google Chrome</a>...</script>

你知道我可以用什么代码来获取正确的数据吗?你知道吗

如果你需要更多的信息,请告诉我。谢谢大家。你知道吗


Tags: 数据comurltitleresponsehtmlchromerequests
2条回答

数据通过JavaScript/JSON动态加载。当您打开Firefox/Chrome开发者工具-网络选项卡时,您将看到页面在哪里发出请求)。你知道吗

此示例将获得有关用户播放器的信息:

import re
import json
import requests
from pprint import pprint
from bs4 import BeautifulSoup


user_data_url = 'https://biwenger.as.com/api/v2/user/4537694?fields=*,account(id),players(id,owner),lineups(round,points,count,position),league(id,name,competition,mode,scoreID),market,seasons,offers,lastPositions'
all_data_url = 'https://cf.biwenger.com/api/v2/competitions/la-liga/data?lang=en&score=1&callback=jsonp_xxx' # < - check @αԋɱҽԃ αмєяιcαη answer, it's possible to do it without callback= parameter

response = requests.get(all_data_url)
data = json.loads( re.findall(r'jsonp_xxx\((.*)\)', response.text)[0] )

user_data = requests.get(user_data_url).json()

# pprint(user_data)  # <  uncomment this to see user data
# pprint(data)       # <  uncomment this to see data about all players

for p in user_data['data']['players']:
    pprint(data['data']['players'][str(p['id'])])
    print('-' * 80)

印刷品:

    {'fantasyPrice': 22000000,
     'fitness': [10, 2, 2, 2, -2],
     'id': 599,
     'name': 'Pedro León',
     'playedAway': 8,
     'playedHome': 8,
     'points': 38,
     'pointsAway': 16,
     'pointsHome': 22,
     'pointsLastSeason': 16,
     'position': 3,
     'price': 1400000,
     'priceIncrement': 60000,
     'slug': 'pedro-leon',
     'status': 'ok',
     'teamID': 76}
                                            
    {'fantasyPrice': 9000000,
     'fitness': [None, 'injured', 'doubt', None, 2],
     'id': 1093,
     'name': 'Javi López',
     'playedAway': 4,
     'playedHome': 2,
     'points': 10,
     'pointsAway': 6,
     'pointsHome': 4,
     'pointsLastSeason': 77,
     'position': 2,
     'price': 210000,
     'priceIncrement': 0,
     'slug': 'javier-lopez',
     'status': 'ok',
     'teamID': 7}
                                            

... and so on.
import requests
import csv

r = requests.get(
    "https://cf.biwenger.com/api/v2/competitions/la-liga/data?lang=en&score=1").json()

data = []
for k, v in r['data']['players'].items():
    data.append(v.values())

with open('output.csv', 'w', newline="", encoding="UTF-8") as f:
    writer = csv.writer(f)
    writer.writerow(v.keys())
    writer.writerows(data)

输出:Click Here

enter image description here

相关问题 更多 >

    热门问题