Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.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 如何使用BeautifulSoup刮取缺少标签的网页_Python_Beautifulsoup - Fatal编程技术网

Python 如何使用BeautifulSoup刮取缺少标签的网页

Python 如何使用BeautifulSoup刮取缺少标签的网页,python,beautifulsoup,Python,Beautifulsoup,我正在尝试从该页面中提取数据: 以下是我正在使用的代码: import requests from bs4 import BeautifulSoup url = "http://www.kitco.com/texten/texten.html" r = requests.get(url) # Doing this to force UFT-8 encoding. Not sure if this is needed... r.encoding = "UTF-8" soup = Beaut

我正在尝试从该页面中提取数据:

以下是我正在使用的代码:

import requests
from bs4 import BeautifulSoup

url = "http://www.kitco.com/texten/texten.html"
r = requests.get(url)

# Doing this to force UFT-8 encoding.  Not sure if this is needed...
r.encoding = "UTF-8"

soup = BeautifulSoup(r.content)
tag = soup.find_all("London Fix")
print tag
正如您在查看该页面的源代码时所注意到的,术语“London Fix”不在任何标记中-我不确定这是cdata还是什么


知道如何解析这些表吗?

正如@shaktimaan在评论中指出的,“London Fix”表不是一个真正的表-它位于
pre
标记内,行是用破折号格式化的

一个选项是在表前找到
font
标记,并获取:

印刷品:

--------------------------------------------------------------------------------
London Fix          GOLD          SILVER       PLATINUM           PALLADIUM
                AM       PM                  AM       PM         AM       PM
--------------------------------------------------------------------------------
Aug 29,2014   1285.75   1285.75   19.4700   1424.00   1424.00   895.00   NA  
Aug 28,2014   1288.00   1292.00   19.7500   1425.00   1428.00   897.00   898.00  
--------------------------------------------------------------------------------
...

另一个选项是搜索(生成相同的输出):


如果您使用的是
r.content
,则不需要设置
r.encoding
。顺便说一句,这是完全正确的。我的结论过于宽泛,但我也可以证明“不清楚你在问什么”,因为你没有指定你期望的输出。我建议你开始更仔细地阅读这篇文章,看看
汤。find_all()
也有什么作用。最后但并非最不重要的是,为什么不改为在解析HTML版本?因为“London Fix”表实际上不是一个HTML表,所以使用此URL可以做的最好的事情就是获取
标记,然后在其上执行
下一个同级操作,以获取整个表
--------------------------------------------------------------------------------
London Fix          GOLD          SILVER       PLATINUM           PALLADIUM
                AM       PM                  AM       PM         AM       PM
--------------------------------------------------------------------------------
Aug 29,2014   1285.75   1285.75   19.4700   1424.00   1424.00   895.00   NA  
Aug 28,2014   1288.00   1292.00   19.7500   1425.00   1428.00   897.00   898.00  
--------------------------------------------------------------------------------
...
import re

print soup.body.pre.find(text=re.compile('London Fix'))