2017-10-13 79 views
0

我有一些问题网站刮美丽的汤一些数据,我想知道如果你们任何刮板专业人士可以给我一些指导。来自coinmarketcap.com的网络刮历史比特币数据

这是确切的网页,我想凑: https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20130428&end=20171013

具体来说,我想抓住历史价格的表格并以某种方式提取信息到数据帧。但首先我需要在原始html中实际找到它。

import requests 
from bs4 import BeautifulSoup 

data = requests.get('https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20130428&end=20171013') 

soup = BeautifulSoup(data._content, 'html.parser') 

不幸的是,我发现了一个编码错误

UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 22075: ordinal not in range(128) 

是有办法基本上只是去掉所有无法通过原始HTML美丽的汤之前进行编码的字符?

回答

0

BeautifulSoup(data._content.decode('utf-8'))

尝试解码utf-8第一。

如果您仍然有问题,你可以告诉解码器忽略错误:

BeautifulSoup(data._content.decode('utf-8', 'ignore))

0

这并不直接回答你的问题 - 我不知道如何设置LXML的解析器,但我成功发现并提取数据 - 注意,是BS内使用LXML的方式,但我直接使用LXML而不是通过BS访问它

from lxml import html 
import requests 


data = requests.get('https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20130428&end=20171013').content 
tree = html.fromstring(data) 
# note I did not want to sort out the logic to find the table so I cheated 
# and selected the table with a specific data value 
mytable = tree.xpath('//td[contains(.,"4829.58")]/ancestor::table')[0] 
for e in mytable.iter(tag='tr'): 
    e.text_content() 

    '\n      Date\n      Open\n      High\n      Low\n      Close\n      Volume\n      Market Cap\n     ' 
    '\n      Oct 12, 2017\n      4829.58\n      5446.91\n      4822.00\n      5446.91\n      2,791,610,000\n      80,256,700,000\n      ' 

我觉得unicode的问题进一步是上树(一些元素OTH呃比你正在寻找的表),所以我没有问题从表中获取数据或将结果写入文件。