2017-11-18 15 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 

あなたは変更する必要があります唯一の事は、私のリクエストで439354だった「後」クエリ文字列パラメータですが、あなたが最初のレコードを取得するために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

関連する問題