Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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 Beautifulsoup4从url提取数据_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python Beautifulsoup4从url提取数据

Python Beautifulsoup4从url提取数据,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正在编写一个python 3.3.3程序,用于从以下网页中获取值:。我以英镑/美元为例。我使用BeautifulSoup 4,我有以下代码: import requests from bs4 import BeautifulSoup url = "http://www.oanda.com/currency/live-exchange-rates/" r = requests.get(url) soup = BeautifulSoup(r.content) g_data = soup.fi

我正在编写一个python 3.3.3程序,用于从以下网页中获取值:。我以英镑/美元为例。我使用BeautifulSoup 4,我有以下代码:

import requests
from bs4 import BeautifulSoup


url = "http://www.oanda.com/currency/live-exchange-rates/"
r = requests.get(url)
soup = BeautifulSoup(r.content)

g_data = soup.find_all("div",{"class": "inline value right"})
for item in g_data:
     print (item.contents)
当我运行它时,我会得到一长串值,其中之一是我感兴趣的英镑/美元值:

['\n', <span class="inline_int" id="GBP_USD-b-int"></span>, <span class="pip"     
id="GBP_USD-b-pip"></span>, <span class="inline_pipette" id="GBP_USD-b-ette"></span>, '\n']

我想知道如何显示这些值(1.65、68和8)。我只需要显示这些值,因为我将使用解析器来提取这些值。

这些值不在页面源代码中。他们将通过单独的请求/响应。请参阅网页中的请求。这些请求是在firebug(firefox)或开发人员工具(chorme)中发出的。由于您使用[0]作为索引,因此您还在
\n
字符上执行
.text
方法。正如您所看到的,列表中的第一项只是一个换行符。@salmanwahed那么Python中有没有办法绕过这个问题并用span中的值提取页面?一种方法是找到网页获取数据的
请求url
。在firebug中,转到
Net
选项卡,或在chorme中转到
Network
选项卡,查看请求。请参阅
XHR
标记中的
xmlhttp
请求并分析响应。如果发现有用的响应,则使用该
请求url
获取数据。
<span id="GBP_USD-b-int" class="inline_int" style="color: rgb(102, 102, 102);">1.65</span>
<span id="GBP_USD-b-pip" class="pip" style="color: rgb(102, 102, 102);">68</span>
<span id="GBP_USD-b-ette" class="inline_pipette" style="color: rgb(102, 102, 102);">8</span>