2016-12-24 13 views
-1

これは私の最初のPythonプロジェクトです。これはかなりYouTubeの動画に続いて書きました。十分な知識はありませんが、私はコーディングの基礎を持っていると思います。私はクロール各ページから Pythonでウェブサイトをクロールした後に特定のデータを取得する

#importing the module that allows to connect to the internet 
import requests 

#this allows to get data from by crawling webpages 
from bs4 import BeautifulSoup 

#creating a loop to change url everytime it is executed 
def creator_spider(max_pages): 
page = 0 
while page < max_pages: 
    url = 'https://www.patreon.com/sitemap/campaigns/' + str(page) 
    source_code = requests.get(url) 

    plain_text = source_code.text 
    soup = BeautifulSoup(plain_text, "html.parser") 

    for link in soup.findAll('a', {'class': ''}): 
     href = "https://www.patreon.com" + link.get('href') 
     #title = link.string 
     print(href) 
     #print(title) 
     get_single_item_data(href) 
    page = page + 1 

def get_single_item_data(item_url): 
    source_code = requests.get(item_url) 
    plain_text = source_code.text 

    soup = BeautifulSoup(plain_text, "html.parser") 
    print soup 
    for item_name in soup.findAll('h6'): 
    print(item_name.string) 

は、私が取得するためのコードは、この情報を強調したい: http://imgur.com/a/e59S9そのソースコードです : http://imgur.com/a/8qv7k

私は(私はsoup.findAllの属性を変更する必要がある数える何) get_single_item_data()functiomでは、すべての試みは無駄でした。これに関する助けは非常に感謝しています。 BS4ドキュメント

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class

から

+1

これはJavaScriptのサイトで、クロールできません。これらの種類のページをクロールするには、実際のブラウザをシミュレートする必要があります。セレンやファントムを試すことができます – sailesh

答えて

0

これは、特定のCSSクラスを持つタグを検索するには非常に便利ですが、CSS属性の名前は、「クラス」、Pythonの予約語です。クラスをキーワード引数として使用すると、構文エラーが発生します。美しいスープ4.1.2の時点で、あなたはキーワード引数class_がを使用してCSSクラスで検索することができます:

soup.find_all("a", class_="sister") 
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, 

しかしあなたは、PICに言及したコードをよく見た後に、このアプローチは、あなたが欲しいものを得ることはありません。ソースでは、data-react-idが表示されます。 DOMはReactJSによってビルドされており、requests.get(url)はJSを最終的に実行しません。ブラウザでJSを無効にして、何が返されたのかをrequests.get(url)で確認してください。

お礼

関連する問題