【问题标题】:Python - How to retrieve certain text from a websitePython - 如何从网站中检索某些文本
【发布时间】:2018-02-25 15:33:25
【问题描述】:

我有以下代码:

import requests
from bs4 import BeautifulSoup
import urllib.request
import urllib.parse
import re

market = 'INDU:IND'
quote_page = 'http://www.bloomberg.com/quote/' + market

page = urllib.request.urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
name_box = soup.find('h1', attrs={'class': 'name'})
name = name_box.text.strip()
print('Market: ' + name)

此代码有效,让我从 url 获取市场名称。我正在尝试做类似于this 网站的事情。这是我的代码:

market = 'BTC-GBP'
quote_page = 'https://uk.finance.yahoo.com/quote/' + market
page = urllib.request.urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
name_box = soup.find('span', attrs={'class': 'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'})
name = name_box.text.strip()
print('Market: ' + name)

我不知道该怎么办。我想检索当前汇率,它增加/减少的数量作为数字和百分比。最后是信息更新的时间。我该怎么做,我不介意您是否使用与我之前使用的方法不同的方法,只要您解释一下即可。如果我的代码效率低下/unpythonic,你能否告诉我如何解决这个问题。我对网络抓取和这些新模块很陌生。谢谢!

【问题讨论】:

  • 你想要什么输出
  • 当前汇率。但我也希望它能够以百分比和数字的形式输出市场的增加/减少,这两者都在网站上。以及信息上传到网站的时间。

标签: python python-3.x beautifulsoup


【解决方案1】:

您可以使用 BeautifulSoup 并在搜索所需数据时,使用正则表达式匹配网站后端脚本生成的动态跨度类名:

from bs4 import BeautifulSoup as soup
import requests
import re

data = requests.get('https://uk.finance.yahoo.com/quote/BTC-GBP').text
s = soup(data, 'lxml')
d = [i.text for i in s.find_all('span', {'class':re.compile('Trsdu\(0\.\d+s\) Trsdu\(0\.\d+s\) Fw\(\w+\) Fz\(\d+px\) Mb\(-\d+px\) D\(\w+\)|Trsdu\(0\.\d+s\) Fw\(\d+\) Fz\(\d+px\) C\(\$data\w+\)')})]
date_published = re.findall('As of\s+\d+:\d+PM GMT\.|As of\s+\d+:\d+AM GMT\.', data) 
final_results = dict(zip(['current', 'change', 'published'], d+date_published))

输出:

{'current': u'6,785.02', 'change': u'-202.99 (-2.90%)', 'published': u'As of  3:55PM GMT.'}

编辑:给定新 URL,您需要更改跨度类名:

data = requests.get('https://uk.finance.yahoo.com/quote/AAPL?p=AAPL').text
final_results = dict(zip(['current', 'change', 'published'], [i.text for i in soup(data, 'lxml').find_all('span', {'class':re.compile('Trsdu\(0\.\d+s\) Trsdu\(0\.\d+s\) Fw\(b\) Fz\(\d+px\) Mb\(-\d+px\) D\(b\)|Trsdu\(0\.\d+s\) Fw\(\d+\) Fz\(\d+px\) C\(\$data\w+\)')})] + re.findall('At close:\s+\d:\d+PM EST', data)))

输出:

{'current': u'175.50', 'change': u'+3.00 (+1.74%)', 'published': u'At close:  4:00PM EST'}

【讨论】:

  • 非常感谢!我不得不用 pip 安装 lxml 但之后它就起作用了。
  • 嘿,当我将 url 的结尾从 BTC-GBP 更改为 AAPL?p=AAPL 时,字典中没有 as of 位。任何想法为什么?在进一步测试之后,有些已经发布了......而有些则没有。你能告诉我如何解决这个问题吗?谢谢!
  • @RandomPerson1234554321 新添加的链接会呈现一个具有不同 span 类名的页面。请查看我最近的编辑。
  • 它仍然打印出这个:{'current': '178.890', 'change': '+3.390 (+1.932%)'}。我试图获取的网站上的文字是“截至美国东部标准时间上午 11:57”。市场开放。 class= 'C($c-fuji-grey-j) D(b) Fz(12px) Fw(n) Mstart(0)--mobpsm Mt(6px)--mobpsm' 并且ID是'quote-market -通知'。
【解决方案2】:

您可以直接使用雅虎财经提供的api, 作为参考,请检查此答案:- Yahoo finance webservice API

【讨论】:

  • 我知道,但我也想将此应用到其他网站,并了解有关网络抓取的更多信息。不过谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2016-04-18
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多