2017-11-18 23 views
0

사이트의 모든 제목 이름을 반환하기 위해 'https://www.kaggle.com/kernels'을 긁어 내려하지만 'div data-reactroot'세부 정보 컨테이너가있는 문제가 발생합니다. 스크래핑 된 데이터로 끌어 들여지지 않습니다.웹 스크랩이 전체 HTML을 반환하지 않음

import urllib 
from bs4 import BeautifulSoup 

kaggle = 'https://www.kaggle.com/kernels' 
data = urllib.request.urlopen(kaggle).read() 
htmlparse = BeautifulSoup(data, 'html.parser') 
print(htmlparse.findAll("div", {"class" : "block-link block-link--bordered"})) 

내 코드에 오류가 있습니까? 아니면 사이트에서이 데이터를 스크래핑하지 못하게 차단하는 블록이 있습니까?

+0

이 정적 콘텐츠 분석을 위해 사용되는 HTML 콘텐츠 URLLIB하지 얻기위한 시간 지연 및 사용 요청 라이브러리를 제공 , 귀하의 사례가 동적 콘텐츠입니다. –

+0

[HTML 데이터가 urllib에 숨겨져 있습니다] 가능한 복제본 (https://stackoverflow.com/questions/47351045/html-data-is-hidden-from-urllib) –

답변

0

페이지를 요청할 때마다 json 형식으로 JavaScript로 가져옵니다. 이렇게하면 "https://www.kaggle.com/kernels.json?sortBy=hotness&group=everyone&pageSize=20&after=439354&language=all&outputType=all"에서 가져올 수 있습니다.

import requests 
import json 
source = requests.get("https://www.kaggle.com/kernels.json?sortBy=hotness&group=everyone&pageSize=20&after=439354&language=all&outputType=all") 
json_obj = source.json() 
for a in json_obj: 
    print (a["title"]) 

출력 :

2004-2005 Landfalling Hurricanes animation 
Visualization of StockData 
Generating Sentences One Letter at a Time 
Decoding the Sexiest Job of 21st Century!! 
Novice to Grandmaster 
Analysis on Pokemon Data 
ROC Curve with k-Fold CV 
Japan Bulgaria trade playground 
Bootstrapping and CIs with Veteran Suicides 
Replicating "Did I do that?" paper analyses with R 
Social Progress Index and World Happiness Report 
SVM+HOG On ColourCompositeImage 
Low- level students 
PyTorch Speech Recognition Challenge (WIP) 
Loans -getting Insights 
Exploring Youtube Trending Statistics EDA 
3 Simple Steps (LB: .9878 with new data) 
Titanic: Neural Network using Keras 
Feature Engineering 
Why do employees leave and what to do about it 

쿼리 문자열 매개 변수 "후"내 요청에 439,354이었다 그러나 첫 번째 기록을 얻기 위해 0으로 설정할 수 변경해야 할 유일한 것은이다 .

"pageSize"쿼리 문자열 매개 변수를 변경하여 반환되는 레코드의 양을 변경할 수도 있습니다. "https://www.kaggle.com/kernels.json?sortBy=hotness&group=everyone&pageSize=5&after=0&language=all&outputType=all"

출력 :

Data ScienceTutorial for Beginners 
Data visualization and investigation 
Spooky NLP and Topic Modelling tutorial 
20 Years Of Games Analysis 
NYC Taxi EDA - Update: The fast & the curious 

또는 URLLIB와 예 :

import urllib.request 
import json 
kaggle = "https://www.kaggle.com/kernels.json?sortBy=hotness&group=everyone&pageSize=5&after=0&language=all&outputType=all" 
data = urllib.request.urlopen(kaggle).read() 
json_obj = json.loads(data.decode("utf-8")) 
for a in json_obj: 
    print (a["title"]) 
0

Elis Byberi으로 작성했기 때문에 문제는 실제로 데이터가 백엔드에서 렌더링되기 전에 데이터를 가져 오려고하는 것입니다. phantomjs를 사용하여 백엔드 작업 후 페이지 내용을 얻을 수 있습니다. 작은 자습서를 찾을 수 있습니다 here

관련 문제