Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:阅读网页并从该网页中提取文本_Python_Html - Fatal编程技术网

Python:阅读网页并从该网页中提取文本

Python:阅读网页并从该网页中提取文本,python,html,Python,Html,我用Python编写本文是为了尝试从网站上获取汇率: xe.com/currency/converter(抱歉,我无法发布其他链接-我已处于极限) 我希望能够从该文件中获取汇率,例如,英镑和美元之间的转换: 因此,我会搜索url:,然后得到打印的值“1.56371USD”(我写这条消息时的费率),并将该值作为int分配给变量,如rate\u USD。 目前,我正在考虑使用BeautifulSoup模块和urllib.request模块,请求url(“”),并使用BeautifulSoup进行搜索

我用Python编写本文是为了尝试从网站上获取汇率: xe.com/currency/converter(抱歉,我无法发布其他链接-我已处于极限) 我希望能够从该文件中获取汇率,例如,英镑和美元之间的转换: 因此,我会搜索url:,然后得到打印的值“1.56371USD”(我写这条消息时的费率),并将该值作为int分配给变量,如rate\u USD。 目前,我正在考虑使用BeautifulSoup模块和urllib.request模块,请求url(“”),并使用BeautifulSoup进行搜索。目前,我正处于编码阶段:

import urllib.request
import bs4 from BeautifulSoup

def rates_fetcher(url):
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html)
    # code to search through soup and fetch the converted value
    # e.g. 1.56371
    # How would I extract this value?
    # I have inspected the page element and found the value I want to be in the class:
    # <td width="47%" align="left" class="rightCol">1.56371&nbsp;
    # I'm thinking about searching through the class: class="rightCol"
    # and extracting the value that way, but how?
url1 = "http://www.xe.com/currencyconverter/convert/?Amount=1&From=GBP&To=USD"
rates_fetcher(url1)
导入urllib.request
从BeautifulSoup导入bs4
def rates_取数器(url):
html=urllib.request.urlopen(url.read())
soup=BeautifulSoup(html)
#用于搜索soup并获取转换值的代码
#例如1.56371
#如何提取该值?
#我已经检查了page元素,并找到了我希望在类中的值:
# 1.56371 
#我正在考虑在类中搜索:class=“rightCol”
#这样提取价值,但如何提取?
url1=”http://www.xe.com/currencyconverter/convert/?Amount=1&From=GBP&To=USD"
取数器(url1)
任何帮助都将不胜感激,并感谢您花时间阅读本文的人


p、 如果我有任何打字错误,我有点赶时间:听起来你的想法是对的

def rates_fetcher(url):
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html)
    return [item.text for item in soup.find_all(class_='rightCol')]
应该可以了。。。这将返回类为“rightCol”的任何标记内的文本列表

如果你没有通读这本书,你真的应该读。这很简单,也很有用。

试试看。这比汤好多了

注意:对于
urllib
,请尝试


PS2:事实上,我最后使用Node和jQuery/jQuery之类的工具来清除html。

我不熟悉BeautifulSoup,你考虑过正则表达式吗?re.findall(r'[0-9]+\[0-9]+USD',html)应该可以工作,但我还没有测试过。哇!感谢您的快速回复!明天早上我就得试试了,因为现在已经是床上告别了……zzz……好吧,别激动。我直到现在才看这个页面,但是当你检查页面元素时,你可能错过了这一点:
我在计算如何获得实时利率时正在考虑这个问题。。。我最初试图从ExchangeGerate-api.com获取汇率,但该网站目前似乎已关闭。否则我怎么才能从法律上获得实时汇率呢?我没有研究过的答案,但一些快速的谷歌搜索出现了。太好了,谢谢你,明天早上我会试试我的汇率获取功能。谢谢你所做的一切。