2017-07-29 13 views
1

私は次のHTMLコードを含むサイトをこすりしようとしている:美しいスープで掻くスキー?

<div class="content-sidebar-wrap"><main class="content"><article 
class="post-773 post type-post status-publish format-standard has-post- 
thumbnail category-money entry" itemscope 
itemtype="http://schema.org/CreativeWork"> 

これは、私が興味のデータが含まれています...私はそれを解析するためにBeautifulSoupを使ってみましたが、次のように戻りました。

<div class="content-sidebar-wrap"><main class="content"><article 
class="entry"> 
<h1 class="entry-title">Not found, error 404</h1><div class="entry-content 
"><p>"The page you are looking for no longer exists. Perhaps you can return 
back to the site's "<a href="http://www.totalsportek.com/">homepage</a> and 
see if you can find what you are looking for. Or, you can try finding it 
by using the search form below.</p><form 
action="http://www.totalsportek.com/" class="search-form" 
itemprop="potentialAction" itemscope="" 
itemtype="http://schema.org/SearchAction" method="get" role="search"> 

# I've made small modifications to make it readable 

美しいスープ要素には、私の希望するコードが含まれていません。私はあまりHTMLに精通していないが、私はこれがデータを返すいくつかの外部サービスへの呼び出しを行うと仮定している..?私はこれを読んだことがあるスキーマと何かがあります。

私はこのデータにアクセスできますか?

+0

HTMLコードから何を取得しますか? –

+0

はHTMLテーブルです。テーブルを解析しようとすると、Noneが返されます。 –

+0

私はまだそれを取得しません。情報を取得しようとしているウェブサイトは正確に何ですか?情報がJavaScriptによって構築されている場合、 '要求'は機能しません。 –

答えて

1

リクエストするときにUser-Agentヘッダーを指定する必要があります。記事ヘッダーとコンテンツを印刷する作業例:

import requests 
from bs4 import BeautifulSoup 

url = "http://www.totalsportek.com/money/barcelona-player-salaries/" 

response = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36"}) 
soup = BeautifulSoup(response.content, "html.parser") 

article = soup.select_one(".content article.post.entry.status-publish") 
header = article.header.get_text(strip=True) 
content = article.select_one(".entry-content").get_text(strip=True) 

print(header) 
print(content) 
+0

コードは機能します。しかし、あなた自身のコードであなたのユーザエージェントを指定しましたが、それでも動作しません。ユーザエージェントを設定する以外に何か必要がありますか?シンプルなsoup.tableを実行すると、少なくとも1つのテーブルが必要な場所は何も返されません。 –

+0

また、私は、テキストとして解析するのではなく、直接htmlテーブルにアクセスすることを好む –

+0

テーブルを手に入れました....あなたのコードを使用します。可能であれば、なぜsoup.findが動作しないのかを知りたい –

関連する問題