2016-11-02 4 views
2
import requests 
from bs4 import BeautifulSoup 

request = requests.get("http://www.lolesports.com/en_US/worlds/world_championship_2016/standings/default") 
content = request.content 
soup = BeautifulSoup(content, "html.parser") 
team_name = soup.findAll('text', {'class': 'team-name'}) 

print(team_name) 

url : "http://www.lolesports.com/en_US/worlds/world_championship_2016/standings/default"에서 데이터를 구문 분석하려고합니다. <text class="team-name">SK Telecom T1</text>에는 개별 팀 이름이 있습니다. 내가하려는 것은이 데이터 (SK Telecom T1)를 구문 분석하여 화면에 인쇄하는 대신 비어있는 목록을 얻는 것입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?BeautifulSoup4로 데이터 구문 분석

답변

2

은, 모든 동적 콘텐츠가 http://api.lolesports.com/api/v1/leagues에 간단한 GET 요청을 JSON 형식으로 검색 할 수 있습니다 : 당신이 원하는 당신에게 데이터의 전체를 많이 제공

import requests 

data = requests.get("http://api.lolesports.com/api/v1/leagues?slug=worlds").json() 

는 것 모두 data["teams"] 이하 여야합니다. 니펫은 :

[{'id': 2, 'slug': 'bangkok-titans', 'name': 'Bangkok Titans', 'teamPhotoUrl': 'http://na.lolesports.com/sites/default/files/BKT_GPL.TMPROFILE_0.png', 'logoUrl': 'http://assets.lolesports.com/team/bangkok-titans-597g0x1v.png', 'acronym': 'BKT', 'homeLeague': 'urn:rg:lolesports:global:league:league:12', 'altLogoUrl': None, 'createdAt': '2014-07-17T18:34:47.000Z', 'updatedAt': '2015-09-29T16:09:36.000Z', 'bios': {'en_US': 'The Bangkok Titans are the undisputed champions of Thailand’s League of Legends esports scene. They achieved six consecutive 1st place finishes in the Thailand Pro League from 2014 to 2015. However, they aren’t content with just domestic domination. 

각 팀은 목록에있는 경우 dicts :

In [1]: import requests 


In [2]: data = requests.get("http://api.lolesports.com/api/v1/leagues?slug=worlds").json() 


In [3]: for d in data["teams"]: 
    ...:   print(d["name"]) 
    ...:  
Bangkok Titans 
ahq e-Sports Club 
SK Telecom T1 
TSM 
Fnatic 
Cloud9 
Counter Logic Gaming 
H2K 
Edward Gaming 
INTZ e-Sports 
paiN Gaming 
Origen 
LGD Gaming 
Invictus Gaming 
Royal Never Give Up 
Flash Wolves 
Splyce 
Samsung Galaxy 
KT Rolster 
ROX Tigers 
G2 Esports 
I May 
Albus NoX Luna 
+0

환상적! 고마워요, 그걸 어떻게 알았습니까? –

2

웹 사이트는 자바 스크립트에 따라 다릅니다. 요청은 JS를 해석하지 않으므로 데이터를 구문 분석 할 수 없습니다.

이와 같은 웹 사이트의 경우 Selenium을 사용하는 것이 좋습니다. 그것은 JS를 포함한 전체 웹 사이트에 대한 인터프리터로 Firefox (또는 다른 드라이버)를 사용합니다. 당신은 셀레늄이 필요하지 않습니다

+1

([셀레늄 PhantomJS 헤드리스 웹킷 통해 긁어] 제공된 코드 http://pastebin.com/ 077jtNDD) – Mangohero1

+0

베르나르도 감사합니다, 셀레늄은 내가 필요한 것입니다. –