2017-02-28 56 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)是我缺少的部分。 –