Python BeautifulSoup 不抓取这个 url

2023-12-04

我试图从网址中抓取一些玩家数据 (tr) 行,但是当我运行代码时似乎没有任何反应。我确信我的代码很好,因为它可以与其他包含表格的统计网站一起使用。谁能告诉我为什么什么都没有发生?提前致谢。

import urllib
import urllib.request
from bs4 import BeautifulSoup

def make_soup(url):
thepage = urllib.request.urlopen(url)
soupdata = BeautifulSoup(thepage, "html.parser")
return soupdata

soup = make_soup("https://www.whoscored.com/Regions/252/Tournaments/7/Seasons/6365/Stages/13832/PlayerStatistics/England-Championship-2016-2017")
for record in soup.findAll('tr'):
    print(record.text)

简短回答: 您要查找的玩家数据是NOT在该网址中。

那么你可能想问why? 我见过他们在那个页面中,为什么他们不在那里?

所以我会尝试解释一下当您浏览该网址时会发生什么使用 Chrome 等现代浏览器。

You:输入网址并按 Enter 键。

Chrome:明白了。我会尽快为您找到该页面,请稍候。 (从该网址获取内容),现在我拥有它了!但等一下让我 在我向你展示之前先阅读/解析它,(阅读里面的内容 内容),哦,糟糕,这个 javascript 告诉我要获取额外的内容 来自另一个网址的信息,好的,我会这样做;哦等等,这是另一个 告诉我在标题中加载广告,我不喜欢它,但是 我只会做我被告知的事情;等一下,这些 css 告诉我 以粗体显示玩家姓名,还不错;哦,这是另一张照片 url xxx 我需要加载,没问题...天哪,有多少东西 给我处理?我对这个网站不满意...(正在开发 一堆其他东西...)终于一切准备就绪!现在检查一下!

You:xxx玩家其实还不错,我去看看。 (点击玩家xxx)

Chrome:: ......

正如您每次浏览网页时所看到的那样,浏览器会执行许多“幕后”操作来向用户显示网页。所以基本上:输入的 url >> 获取的 url 内容 >> 解析的内容 >> 获取的其他内容 >> 呈现的所有内容 >> 显示的页面(一个或多个步骤可以同时完成)

并且使用您的代码,它只是“从 url 获取的内容”,也您想要的那些统计数据恰好是“附加内容”,必须从其他地方加载,所以这就是为什么你一无所获。

那么我如何获得这些统计数据呢?一旦您知道负责加载这些统计信息的网址,只需跟踪它们即可。我如何找到这些网址?好吧,你总是可以阅读 JavaScript...如果你足够耐心的话...

得到你想要的最简单的方法就是分析该页面加载时的流量,并找出所有幕后流量。我会推荐fiddler,但您可以使用任何您认为合适的工具。

Now let's see what happens when you load that page: traffic analytics

实际上,有数百个请求来完全呈现您访问的页面,您所需要做的就是找出哪一个提供“实际”或“真实”统计数据。即使其中包含“StatisticsFeed”,也有一个 url,它可能就是这个吗?让我们来看看:

{
    "playerTableStats": [{
        "name": "Conor Hourihane",
        "firstName": "Conor",
        "lastName": "Hourihane",
        "playerId": 134172,
        "height": 181,
        "weight": 62,
        "age": 25,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-MC-",
        "positionText": "Midfielder",
        "playedPositionsShort": "M(C)",
        "teamId": 142,
        "teamName": "Barnsley",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "ie",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.8705882352941181,
        "ranking": 1,
        "apps": 17,
        "subOn": 0,
        "minsPlayed": 1530,
        "manOfTheMatch": 4,
        "yellowCard": 5.0,
        "redCard": 0.0,
        "goal": 3,
        "assistTotal": 8,
        "shotsPerGame": 2.2352941176470589,
        "aerialWonPerGame": 0.6470588235294118,
        "passSuccess": 81.370449678800867
    },
    {
        "name": "Anthony Knockaert",
        "firstName": "Anthony",
        "lastName": "Knockaert",
        "playerId": 86794,
        "height": 172,
        "weight": 69,
        "age": 25,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-AML-AMR-",
        "positionText": "Midfielder",
        "playedPositionsShort": "AM(LR)",
        "teamId": 211,
        "teamName": "Brighton",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "fr",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.6722222222222216,
        "ranking": 2,
        "apps": 18,
        "subOn": 1,
        "minsPlayed": 1471,
        "manOfTheMatch": 5,
        "yellowCard": 4.0,
        "redCard": 0.0,
        "goal": 6,
        "assistTotal": 0,
        "shotsPerGame": 2.3888888888888888,
        "aerialWonPerGame": 0.22222222222222221,
        "passSuccess": 83.420593368237348
    },
    {
        "name": "Lewis Dunk",
        "firstName": "Lewis",
        "lastName": "Dunk",
        "playerId": 86441,
        "height": 192,
        "weight": 88,
        "age": 25,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-DC-",
        "positionText": "Defender",
        "playedPositionsShort": "D(C)",
        "teamId": 211,
        "teamName": "Brighton",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "gb-eng",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.660000000000001,
        "ranking": 3,
        "apps": 18,
        "subOn": 0,
        "minsPlayed": 1620,
        "manOfTheMatch": 3,
        "yellowCard": 8.0,
        "redCard": 0.0,
        "goal": 1,
        "assistTotal": 1,
        "shotsPerGame": 0.61111111111111116,
        "aerialWonPerGame": 3.5,
        "passSuccess": 79.72251867662753
    },
    {
        "name": "Tom Clarke",
        "firstName": "Tom",
        "lastName": "Clarke",
        "playerId": 133974,
        "height": 180,
        "weight": 77,
        "age": 28,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-DC-",
        "positionText": "Defender",
        "playedPositionsShort": "D(C)",
        "teamId": 181,
        "teamName": "Preston",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "gb-eng",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.6126315789473677,
        "ranking": 4,
        "apps": 19,
        "subOn": 0,
        "minsPlayed": 1692,
        "manOfTheMatch": 4,
        "yellowCard": 0.0,
        "redCard": 0.0,
        "goal": 2,
        "assistTotal": 0,
        "shotsPerGame": 0.89473684210526316,
        "aerialWonPerGame": 5.4736842105263159,
        "passSuccess": 66.666666666666657
    },
    {
        "name": "Pontus Jansson",
        "firstName": "Pontus",
        "lastName": "Jansson",
        "playerId": 121123,
        "height": 194,
        "weight": 89,
        "age": 25,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-DC-",
        "positionText": "Defender",
        "playedPositionsShort": "D(C)",
        "teamId": 19,
        "teamName": "Leeds",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "se",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.5976923076923066,
        "ranking": 5,
        "apps": 13,
        "subOn": 0,
        "minsPlayed": 1126,
        "manOfTheMatch": 1,
        "yellowCard": 6.0,
        "redCard": 0.0,
        "goal": 1,
        "assistTotal": 0,
        "shotsPerGame": 0.53846153846153844,
        "aerialWonPerGame": 3.5384615384615383,
        "passSuccess": 86.336633663366342
    },
    {
        "name": "Angus MacDonald",
        "firstName": "Angus",
        "lastName": "MacDonald",
        "playerId": 110825,
        "height": 184,
        "weight": 70,
        "age": 24,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-DC-",
        "positionText": "Defender",
        "playedPositionsShort": "D(C)",
        "teamId": 142,
        "teamName": "Barnsley",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "gb-eng",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.5066666666666677,
        "ranking": 6,
        "apps": 12,
        "subOn": 0,
        "minsPlayed": 1080,
        "manOfTheMatch": 0,
        "yellowCard": 3.0,
        "redCard": 0.0,
        "goal": 0,
        "assistTotal": 0,
        "shotsPerGame": 0.33333333333333331,
        "aerialWonPerGame": 4.833333333333333,
        "passSuccess": 72.147651006711413
    },
    {
        "name": "Marc Roberts",
        "firstName": "Marc",
        "lastName": "Roberts",
        "playerId": 138949,
        "height": 183,
        "weight": 81,
        "age": 26,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-DC-",
        "positionText": "Defender",
        "playedPositionsShort": "D(C)",
        "teamId": 142,
        "teamName": "Barnsley",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "gb-eng",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.503125,
        "ranking": 7,
        "apps": 16,
        "subOn": 0,
        "minsPlayed": 1440,
        "manOfTheMatch": 1,
        "yellowCard": 3.0,
        "redCard": 0.0,
        "goal": 2,
        "assistTotal": 2,
        "shotsPerGame": 0.625,
        "aerialWonPerGame": 7.0625,
        "passSuccess": 61.595547309833023
    },
    {
        "name": "Bradley Johnson",
        "firstName": "Bradley",
        "lastName": "Johnson",
        "playerId": 12490,
        "height": 178,
        "weight": 68,
        "age": 29,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-MC-ML-",
        "positionText": "Midfielder",
        "playedPositionsShort": "M(CL)",
        "teamId": 20,
        "teamName": "Derby",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "gb-eng",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.4954545454545443,
        "ranking": 8,
        "apps": 11,
        "subOn": 0,
        "minsPlayed": 952,
        "manOfTheMatch": 1,
        "yellowCard": 4.0,
        "redCard": 0.0,
        "goal": 2,
        "assistTotal": 1,
        "shotsPerGame": 1.3636363636363635,
        "aerialWonPerGame": 4.0909090909090908,
        "passSuccess": 71.908127208480565
    },
    {
        "name": "Christophe Berra",
        "firstName": "Christophe",
        "lastName": "Berra",
        "playerId": 8287,
        "height": 186,
        "weight": 81,
        "age": 31,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-DC-",
        "positionText": "Defender",
        "playedPositionsShort": "D(C)",
        "teamId": 165,
        "teamName": "Ipswich",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "gb-sct",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.4789473684210526,
        "ranking": 9,
        "apps": 19,
        "subOn": 0,
        "minsPlayed": 1710,
        "manOfTheMatch": 3,
        "yellowCard": 4.0,
        "redCard": 0.0,
        "goal": 0,
        "assistTotal": 1,
        "shotsPerGame": 0.94736842105263153,
        "aerialWonPerGame": 6.2105263157894735,
        "passSuccess": 58.636363636363633
    },
    {
        "name": "Adam Webster",
        "firstName": "Adam",
        "lastName": "Webster",
        "playerId": 109922,
        "height": 191,
        "weight": 0,
        "age": 21,
        "isManOfTheMatch": false,
        "isActive": true,
        "isOpta": true,
        "playedPositions": "-DC-",
        "positionText": "Defender",
        "playedPositionsShort": "D(C)",
        "teamId": 165,
        "teamName": "Ipswich",
        "seasonId": 6365,
        "seasonName": "2016/2017",
        "tournamentId": 7,
        "tournamentRegionId": 252,
        "tournamentRegionCode": "gb-eng",
        "regionCode": "gb-eng",
        "tournamentName": "Championship",
        "tournamentShortName": "EC",
        "rating": 7.4780000000000006,
        "ranking": 10,
        "apps": 15,
        "subOn": 1,
        "minsPlayed": 1227,
        "manOfTheMatch": 2,
        "yellowCard": 1.0,
        "redCard": 0.0,
        "goal": 0,
        "assistTotal": 0,
        "shotsPerGame": 0.2,
        "aerialWonPerGame": 5.0666666666666664,
        "passSuccess": 58.256029684601117
    }],
    "paging": {
        "currentPage": 1,
        "totalPages": 34,
        "resultsPerPage": 10,
        "totalResults": 338,
        "firstRecordIndex": 1,
        "lastRecordIndex": 10
    },
    "statColumns": ["apps",
    "subOn",
    "minsPlayed",
    "goal",
    "assistTotal",
    "yellowCard",
    "redCard",
    "shotsPerGame",
    "passSuccess",
    "aerialWonPerGame",
    "manOfTheMatch"]
}

确切地!那么现在怎么办?模拟这个请求并解析内容,因为它已经是 JSON 格式,内置模块json会很容易地完成这项工作,你甚至不必使用BeautifulSoup

你可能会问,为什么我直接浏览这个链接什么也没有呢?这是因为他们在服务器上设置了限制,以便只有具有有效标头的请求才会获得提要。那么我该如何绕过它呢?模拟“生动”使用正确的参数(主要是标题),以便他们相信你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python BeautifulSoup 不抓取这个 url 的相关文章

随机推荐

  • 将字符串列表与可用的字典/同义词库进行比较

    我有一个程序 C 它生成一个字符串列表 原始字符串的排列 大多数字符串是按预期随机分组的原始字母 即 etam aemt team 我想以编程方式找到列表中真正的英语单词的一个字符串 我需要一个同义词库 字典来查找和比较每个字符串 任何人都
  • 将 CSS/HTML 中的按钮与文本水平对齐一行?

    我想知道如何水平对齐按钮 最重要的是 我想向这些按钮添加文本 这就是我目前所拥有的 HTML 正文部分 div class tile div table class tile table tr td img class tile image
  • CSS 内部 id 选择器

    我在 CSS 文件中有以下 id 选择器 headerphoto 和 logo box 是 header id 的内部 id 选择器 我真的需要将这两个添加为内部 id 选择器吗 我可以将它们添加为普通的 id 选择器 这样做的最佳方法是什
  • 读取 Android 应用程序的 PKCS12/P12 客户端证书文件

    我正在尝试在我的 Android 应用程序中使用客户端证书 以便我可以确保仅与使用该应用程序或拥有该证书的人进行 HTTPS 通信 我的 res raw 文件夹中有证书 当我开始输入 R raw 时 Android Studio 就会看到它
  • 将缺失值替换为 n-1

    例如 我有 df pd DataFrame 0 420 np nan 455 np nan np nan np nan df 0 0 420 0 1 NaN 2 455 0 3 NaN 4 NaN 5 NaN 然后使用 df 0 isnul
  • 如何在多行中显示长文本的表视图节标题?

    我正在使用具有多个部分的表视图控制器 其中一些部分具有长文本 当我使用 titleForHeaderSection 时 如果文本长度超过表格视图框架 文本就会被截断 我想在下一行显示文本 而不是使用titleForHeaderInSecti
  • “星云”类不存在

    我在处理 3 3 时遇到问题 我刚刚开始开发一种星云模拟器 旨在模拟恒星从星云到红巨星的诞生和生命周期 到目前为止 我已经创建了两个类 Gas 针对每个单独的气体粒子 和 Nebula 指粒子的集合 我在编辑器中输入了以下代码 每次都得到相
  • 如何使用 Javascript 和 JQuery 操作 URL?

    我想制作一个包含大量 Javascript 交互的页面 但是 当用户浏览页面时 URL 也必须更改 因此 当用户共享 URL 或保存 URL 时 它可以引导他到达他的实际状态 我怎样才能做到这一点 例子 myapp com page1 my
  • 如何避免 DbgCommand 命令被写入日志文件

    我有一个恼人的问题Pykd pyd 我在脚本中使用它 启动了几个DbgCommand函数 例如 DbgCommand dt 0x000000eab8748430 CMap
  • C 语言中的正则表达式使用函数 regcomp 和 regexec 在第一个和第二个匹配之间切换

    我正在使用 Dev c IDE 编译我的 C WIN32 API 程序 我正在使用提供的正则表达式库http gnuwin32 sourceforge net packages regex htm 我正在使用此文档作为参考 上面的站点也提供
  • 我可以将表单设置为单例吗?

    我有一个 Visual C 2010 应用程序 它有一个名为MainWnd与其他工具窗口和对话框 我希望其他工具窗口能够与主窗体 对话 并调用其方法 但这需要一个实例MainWnd 并且由于在任何给定时间只会创建其中一种形式 因此没有理由我
  • 使用 matplotlib 计算 2 个椭圆的重叠区域

    有谁知道是否可以使用计算两个椭圆的重叠面积matplotlib patches Ellipse 我必须像这样省略 我想计算重叠面积与各个椭圆的面积之间的比率 仅使用这可能吗Ellipse from matplotlib patches 您无
  • 打开文件对话框在 Swift 中崩溃

    我想使用 NSFilemanager 中的打开文件对话框 但我的代码有时会崩溃 有时会工作 我不知道为什么 有时它可以 100 工作 有时窗口是空的 有时对话框后面的背景显示在窗口中 当发生崩溃时 Xcode 中会显示 signal SIG
  • akka-http 发送连续的分块 http 响应(流)

    我有这个粗略的测试示例akka http客户端和服务器 服务器 scala import akka actor ActorSystem import akka stream ActorMaterializer import akka str
  • 如何在选项标签中写入多种颜色的文本

    我有一个表单中的下拉列表 选择标签 其内容是城市名称 但我也想在他们旁边显示他们的国家名称作为评论 之间可以写文字吗
  • 调试目标丢失?

    当我尝试构建解决方案时 出现以下错误 Visual Studio 无法开始调试 因为缺少调试目标 c target 请构建项目并重试 或者适当设置 OutputPath 和 AssemblyName 属性以指向目标程序集的正确位置 我的输出
  • R 中的 h2o.predict 错误

    我在尝试使用 R 中的 h2o 创建深度学习预测时遇到错误 使用命令 h2o predict 进行的预测中约有三分之一会发生该错误 这是模型设置 localH2O h2o init ip localhost port 54321 start
  • Java游戏中的碰撞检测?

    我正在开发一款游戏 其中存在运动图像碰撞检测的问题 游戏有一艘宇宙飞船和一些小行星 障碍物 我想检测它们之间的碰撞 我怎样才能做到这一点 对于矩形以外的任何物体 碰撞检测通常都很棘手 我过去这样做的方法是为每个对象提供图像和蒙版 例如 像木
  • 在 ffmpeg 中采样精确的音频切片?

    我需要将 wav 格式的音频文件切成 10 秒的块 这些块必须正好是 10 秒 而不是 10 04799988232 秒 我当前使用的代码是 ffmpeg i test wav ss 0 to 10 c a libfdk aac b a 8
  • Python BeautifulSoup 不抓取这个 url

    我试图从网址中抓取一些玩家数据 tr 行 但是当我运行代码时似乎没有任何反应 我确信我的代码很好 因为它可以与其他包含表格的统计网站一起使用 谁能告诉我为什么什么都没有发生 提前致谢 import urllib import urllib