【问题标题】:Sending GET request to URL with fragment returns the content of the main page向带有片段的 URL 发送 GET 请求会返回主页的内容
【发布时间】:2022-01-10 13:57:02
【问题描述】:

我正在尝试对这个webpage 进行网络报废,但我总是最终得到"main" page(相同的URL,但最后没有“#face-a-face”)。和这个人遇到的问题一样,看这个forum。他得到了答案,但我无法概括并将其应用于我要废弃的网站。

import requests
from bs4 import BeautifulSoup

url_main = "https://www.lequipe.fr/Football/match-direct/ligue-1/2020-2021/ol-dijon-live/477168"
url_target = url_main + "#face-a-face"
soup_main = BeautifulSoup(requests.get(url_main, verify=False).content, "html.parser")
soup_target = BeautifulSoup(requests.get(url_target, verify=False).content, "html.parser")
print(soup_main == soup_target)

返回True。我想得到不同的内容,这里不是这样。

例如,我想提取目标网页中的所有“confrontations depuis 2011”。如何通过 GET 请求(或其他方式)获得此 webpage 的最终内容?谢谢!

【问题讨论】:

  • 该网站上有很多 JavaScript 代码。使用 Selenium 可能会更好

标签: python python-3.x web-scraping beautifulsoup python-requests


【解决方案1】:

所有数据都来自一个高度嵌套的JSON 文件。

您可以获得该文件并提取您需要的信息。

方法如下:

import json

import requests

endpoint = "https://iphdata.lequipe.fr/iPhoneDatas/EFR/STD/ALL/V2/Football/Prelive/68/477168.json"

team_data = requests.get(endpoint).json()
specifics = team_data["items"][1]["objet"]["matches"][0]["specifics"]

print(json.dumps(specifics, indent=2))

这应该会给你一本字典:

{
  "__type": "specifics_sport_collectif",
  "vainqueur": "domicile",
  "score": {
    "__type": "score",
    "exterieur": "1",
    "domicile": "4"
  },
  "exterieur": {
    "__type": "effectif_sport_collectif",
    "equipe": {
      "__type": "equipe",
      "id": "202",
      "url_image": "https://medias.lequipe.fr/logo-football/202/{width}{declinaison}",
      "nom": "Dijon",
      "url_fiche": "https://www.lequipe.fr/Football/FootballFicheClub202.html"
    }
  },
  "domicile": {
    "__type": "effectif_sport_collectif",
    "equipe": {
      "__type": "equipe",
      "id": "22",
      "url_image": "https://medias.lequipe.fr/logo-football/22/{width}{declinaison}",
      "nom": "Lyon",
      "url_fiche": "https://www.lequipe.fr/Football/FootballFicheClub22.html"
    }
  },
  "is_final": false,
  "prolongation": false,
  "vainqueur_final": "domicile",
  "is_qualifier": false
}

例如,如果您只想要 socre,请添加以下行:

just_the_score = specifics["score"]
print(just_the_score)

要得到这个:

{'__type': 'score', 'exterieur': '1', 'domicile': '4'}

【讨论】:

猜你喜欢
  • 2020-07-18
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2020-10-02
  • 2016-03-03
  • 2020-08-27
  • 2017-01-14
  • 2021-07-22
相关资源
最近更新 更多