https://vip.stock.finance.sina.com.cn/mkt/#lwg_qh
提示以下错误
s = s.decode(detect_encoding(s), 'surrogatepass')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 184: invalid continuation byte
https://vip.stock.finance.sina.com.cn/mkt/#lwg_qh
提示以下错误
s = s.decode(detect_encoding(s), 'surrogatepass')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 184: invalid continuation byte
编码格式错误,但不知道你是怎么爬的,没办法定位错误
from urllib import request
import json
import pandas as pd
def get_data(id):
url_30m = 'https://vip.stock.finance.sina.com.cn/mkt/#'
url = url_30m + id
req = request.Request(url)
rsp = request.urlopen(req)
res = rsp.read()
res_json = json.loads(res)
bar_list = []
res_json.reverse()
for line in res_json:
print(line)
bar = {}
bar['Datetime'] = line[0]
bar['Open'] = float(line[1])
bar['High'] = float(line[2])
bar['Low'] = float(line[3])
bar['Close'] = float(line[4])
bar['Volume'] = int(line[5])
bar_list.append(bar)
df = pd.DataFrame(data=bar_list)
df.to_csv('d:/Sina data/data.csv', index=None)
return df[-20:-1]
if name == 'main':
get_data('lwg_qh')
decode的时候可以用gb2312试试