【问题标题】:Python Webscraping Vue ComponentsPython Web Scraping Vue 组件
【发布时间】:2020-09-01 19:13:03
【问题描述】:

我试图将图片中标记的值提取为变量,但似乎当它在 Vue 组件中时,bs4 并没有像我期望的那样进行搜索。谁能指出我如何在 Python 中从该文档中提取值的大致方向? 代码在图片下方,在此先感谢。

import requests
from bs4 import BeautifulSoup

URL = 'https://api.tracker.gg/api/v2/rocket-league/standard/profile/steam/76561198060134880'

headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}

page = requests.get(URL, headers = headers)

soup = BeautifulSoup(page.content, 'html.parser')

#print(soup.prettify())

div_list = soup.findAll({"class":'value'})

print(div_list)

【问题讨论】:

    标签: python web-scraping beautifulsoup python-requests


    【解决方案1】:

    由于页面返回的是 json 响应,因此您不需要 beautifulsoup 来解析它。

    import requests
    import json
    
    URL = 'https://api.tracker.gg/api/v2/rocket-league/standard/profile/steam/76561198060134880'
    
    headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}
    
    response = requests.get(URL, headers = headers)
    
    dict_of_response = json.loads(response.text)
    
    obj_list = dict_of_response['data']['segments']
    
    print(obj_list)
    

    obj_list 变量现在包含一个字典列表。这些 dicts 包含您想要的数据,现在您只需要遍历列表并对数据执行您想要的操作。

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 2018-04-02
      • 2017-09-06
      • 2019-06-03
      • 1970-01-01
      • 2021-09-03
      • 2021-12-25
      • 2021-03-12
      相关资源
      最近更新 更多