2017-02-28 6 views
0

ページ上のすべての記事のティーザータイトルを返そうとしています。どのページを検索しても下のコードが表示されても、私はrequests.getとBeautifulSoupを使用してページからタイトルを返します。

終了コード0で終了します。

誰かが間違っている場所を教えてください。私はPyCharm 2016.3.2とAnaconda3を使います。

おかげ

import requests 
from bs4 import BeautifulSoup 

    if __name__ == "__main__": 
    # User agent to bypass scraping security 
    agent = {'User-Agent': 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405'} 
    req = requests.get("http://www.zerohedge.com/", agent) 

    #req.content = html page source and we are using the html parser 
    soup = BeautifulSoup(req.content, "html.parser") 

    for i in soup.find_all("title teaser-title"): 
     print(i.text) 

答えて

1

あなたは、必要に応じて検索し、そのクラスをしたいタグを指定する必要があります。このように:

soup.find_all("h2", class_="title teaser-title") 

または使用cssselector

soup.select("h2[class='title teaser-title']") 
+0

おかげ@Zroqは、(H2)クラスタグは、私が行方不明になった部分でした。 –

関連する問題