2016-08-11 6 views
1

ウェブクロール中にいくつかの困難に遭遇しました。私はいくつかのhtmlの真ん中に埋め込まれたコードのこのチャンクで70を取得しようとしています、私の質問はどうすればそうするかと思います。私は様々な方法を試みましたが、うまくいかないようです。私はBeautifulSoupモジュールを使用していて、Python 3で書いています。誰かがそれを必要とするならば、私が掻いているウェブサイトへのリンクは便利です。a hrefタグ内のデータを取得する方法

<a href="http://www.accuweather.com/en/gb/london/ec4a-2/weather- forecast/328328">London, United Kingdom<span class="temp">70&deg;</span><span class="icon i-33-s"></span></a> 

from bs4 import* 
import requests 
data = requests.get("http://www.accuweather.com/en/gb/london/ec4a-2/weather- forecast/328328") 
soup = BeautifulSoup(data.text,"html.parser") 

答えて

-1
from bs4 import BeautifulSoup 
import re 
import requests 
soup = BeautifulSoup(text,"html.parser") 
for link in soup.find("a") 
    temp = link.find("span",{"class" : "temp"}) 
    print(re.findall(r"[0-9]{1,2}",temp.text)) 

私は、これはBeautifulSoupを使用して、厳格な要件ではないと仮定すると、あなたに

+0

ご意見ありがとうございます!しかし、それはすべてのリンクを印刷し、そのタグで "70"を取得しようとしています – goimpress

0

を役に立てば幸い、あなたはhtml.parserモジュールでこれを行うことができます。以下は、あなたが言及したユースケース用に設計されたカスタムです。 これは両方のデータフィールドを取り出し、その番号をフィルタリングします。

from html.parser import HTMLParser 

class MyHTMLParser(HTMLParser): 
    def handle_data(self, data): 
     if data.isdigit(): 
      print(data) 

parser = MyHTMLParser() 

parser.feed('<a href="http://www.accuweather.com/en/gb/london/ec4a-2/weather- forecast/328328">London, United Kingdom<span class="temp">70&deg;</span><span class="icon i-33-s"></span></a>') 

この意志出力70

も正規表現を使用して行うことができます。これはあなたの温度

temps = soup.find_all('span',{'class':'temp'}) 

を含む任意のスパンを取得します

+0

それはまた、このように行うことができますが、私は送信天気と70の天気のウェブサイトを傷つけようとしていますいくつかのhtmlの真ん中に – goimpress

0

あなたはpython2である場合、ループ

for span in temps: 
    temp = span.decode_contents() 
    # temp looks like "70&deg" or "70\xb0" so parse it 
    print int(temp[:-1]) 

それ以上のハードワークは、おそらくASCIIにUnicodeから変換されます。

しかしアキュウェザーページは、クラスの一時とスパンを持っていません。

In [12]: soup.select('[class~=temp]') 
Out[12]: 
[<strong class="temp">19<span>\xb0</span></strong>, 
<strong class="temp">14<span>\xb0</span></strong>, 
<strong class="temp">24<span>\xb0</span></strong>, 
<strong class="temp">23<span>\xb0</span></strong>, 
<h2 class="temp">19\xb0</h2>, 
<h2 class="temp">19\xb0</h2>, 
<h2 class="temp">17\xb0</h2>, 
<h2 class="temp">19\xb0</h2>, 
<h2 class="temp">19\xb0</h2>, 
<h2 class="temp">19\xb0</h2>, 
<h2 class="temp">20\xb0</h2>, 
<h2 class="temp">19\xb0</h2>, 
<h2 class="temp">17\xb0</h2>, 
<h2 class="temp">19\xb0</h2>, 
<h2 class="temp">19\xb0</h2>] 

だから、あなたが取得するためにユーザーエージェントを追加する必要が答え

+0

最初にそれはうまくいったように見えたが、それはdidtnt – goimpress

+0

何がうまくいかなかったか? – kdopen

+0

もちろん、accu-weatherのそのページは、もはやクラスtempのスパンを使用しません。代わりに 'h2'と 'strong'を使用しています – kdopen

0

を与えるために難しくなって、その後、あなたがしたいタグ/クラス名を使用して選択し、正しいソースを:

:私たちは、コードを実行した場合

from bs4 import * 
import requests 
headers = {"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"} 
data = requests.get("http://www.accuweather.com/en/gb/london/ec4a-2/weather-forecast/328328", headers=headers) 
soup = BeautifulSoup(data.content) 
print(soup.select_one("span.local-temp").text) 
print([span.text for span in soup.select("span.temp")]) 

、あなたは私たちが必要とするすべてを見ます

In [17]: headers = { 
    ....:  "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"} 

In [18]: data = requests.get("http://www.accuweather.com/en/gb/london/ec4a-2/weather-forecast/328328", headers=headers) 

In [19]: soup = BeautifulSoup(data.content, "html.parser") 

In [20]: print(soup.find("span", "local-temp").text) 
18°C 

In [21]: print("\n".join([span.text for span in soup.select("span.temp")])) 
18° 
31° 
30° 
25° 
+0

というBro!これはうまくいった。ありがとうございました – goimpress

+0

右クリックして表示ソースを選択すると、ブラウザの実際のソースと要求から返されたソースを確認するのには心配はありません。 –

+0

私はいくつかの質問がありますが、ユーザーエージェントは何ですか、なぜあなたはそれを必要としますか?これらのコード行は何をするのですか?soup.select_one( "span.local-temp")。textとprint([span.text soup.select(span.temp))) – goimpress

関連する問題