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)
感謝@Zroq,類標籤(H2)是我缺少的部分。 –