无法从福布斯那里得到一天的报价

时间:2016-06-30 17:24:51

标签: python web-scraping beautifulsoup

我正在尝试使用python + Beautiful Soup从福布斯网站上搜集当天的报价。

import requests
from bs4 import BeautifulSoup
import urllib.request

url = requests.get('http://www.forbes.com/quotes/1/')
url = url.text
soup = BeautifulSoup(url,'html.parser')
print(soup.find('p',{'class' : 'ng-binding'}))

当我运行此代码时,我得到输出:无

1 个答案:

答案 0 :(得分:2)

引用本身是从不同的资源URL动态加载。发出请求而不是解析JSON响应:

>>> import requests
>>>
>>> url = "http://www.forbes.com/forbesapi/thought/uri.json?enrich=true&query=1&relatedlimit=5"
>>> response = requests.get(url)
>>> data = response.json()
>>>
>>> data['thought']['quote'].strip()
u'Teach self-denial and make its practice pleasure, and you can create for the world a destiny more sublime that ever issued from the brain of the wildest dreamer.'