使用find()使用漂亮的汤刮网时,“ None”返回

时间:2018-10-04 13:34:04

标签: web-scraping beautifulsoup find python-requests

我正在尝试使用BeautifulSoup&Requests从BBC网站选择FTSE价格,但是运行时我得到的输出为“ None”。

import sys
import requests
from bs4 import BeautifulSoup

URL = 'https://www.bbc.co.uk/news/topics./c9qdqqkgz27t/ftse-100'
page = requests.get(URL,timeout=5)
#fetch content from URL
soup = BeautifulSoup(page.content,'html.parser')
#parse html content

price =  soup.find(class_='gel-paragon nw-c-md-market-summary_value')
#price = soup.find("div", class_="gel-paragon nw-c-md-market-summary_value")
#find class with name 'gel...'
print(price)

我尝试使用不同类型的find函数,但是两者都返回相同的值。我计划最终使用这种逻辑从多个页面收集数据,但希望在尝试进行迭代之前正确处理它。

2 个答案:

答案 0 :(得分:2)

这很完美:

import requests
from bs4 import BeautifulSoup

url = 'https://www.bbc.com/news/topics/c9qdqqkgz27t/ftse-100'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
price = soup.select_one('div.gel-paragon')
print(price.text)

输出:

7418.34

注意:如果您没有“ lxml”,请尝试使用“ html.parser”

答案 1 :(得分:0)

您的网址是错误的,我进行了少量编辑,并且有效!

import requests
from bs4 import BeautifulSoup

URL = 'https://www.bbc.co.uk/news/topics/c9qdqqkgz27t/ftse-100'
page = requests.get(URL)
soup = BeautifulSoup(page.content,'html.parser')
price =  soup.find('div', attrs={
    'class':'gel-paragon nw-c-md-market-summary__value'})
print(price.text)

输出:

7442.28