requests碰到 问题,请大佬看下
爬到一半时出现这个错误 :Traceback (most recent call last):
File "D:/PycharmProjects/sina_data/main1127.py", line 120, in <module>
get_gplist(key, value)
File "D:/PycharmProjects/sina_data/main1127.py", line 106, in get_gplist
gg_data = gg_json.json() # 转换成字典
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\simplejson\__init__.py", line 525, in loads
return _default_decoder.decode(s)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\simplejson\decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\simplejson\decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\simplejson\scanner.py", line 79, in scan_once
return _scan_once(string, idx)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\simplejson\scanner.py", line 70, in _scan_once
raise JSONDecodeError(errmsg, string, idx)
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
请大佬看下怎么处理??感谢 gg_data = gg_json.json() gg_data得到的值 是这个
[{'symbol': 'sh603219', 'code': '603219', 'name': '富佳股份', 'trade': '26.850', 'pricechange': 2.69, 'changepercent': 10.019, 'buy': '29.540', 'sell': '29.540', 'settlement': '26.850', 'open': '0.000', 'high': '0.000', 'low': '0.000', 'volume': 0, 'amount': 0, 'ticktime': '09:17:09', 'per': 55.938, 'pb': 10.354, 'mktcap': 1076685, 'nmc': 110085, 'turnoverratio': 0}, {'symbol': 'sh603355', 'code': '603355', 'name': '莱克电气', 'trade': '30.130', 'pricechange': 0.26, 'changepercent': 0.863, 'buy': '30.390', 'sell': '30.390', 'settlement': '30.130', 'open': '0.000', 'high': '0.000', 'low': '0.000', 'volume': 0, 'amount': 0, 'ticktime': '09:17:03', 'per': 37.198, 'pb': 5.404, 'mktcap': 1732016.1201, 'nmc': 1699308.1973, 'turnoverratio': 0}, {'symbol': 'sh603486', 'code': '603486', 'name': '科沃斯', 'trade': '162.780', 'pricechange': 1.22, 'changepercent': 0.749, 'buy': '164.000', 'sell': '164.000', 'settlement': '162.780', 'open': '0.000', 'high': '0.000', 'low': '0.000', 'volume': 0, 'amount': 0, 'ticktime': '09:17:03', 'per': 142.789, 'pb': 22.059, 'mktcap': 9311404.63725, 'nmc': 9138398.79765, 'turnoverratio': 0}, {'symbol': 'sh605555', 'code': '605555', 'name': '德昌股份', 'trade': '47.490', 'pricechange': -1.91, 'changepercent': -4.022, 'buy': '45.580', 'sell': '45.580', 'settlement': '47.490', 'open': '0.000', 'high': '0.000', 'low': '0.000', 'volume': 0, 'amount': 0, 'ticktime': '09:17:08', 'per': 21.885, 'pb': 9.171, 'mktcap': 902215.02, 'nmc': 237450, 'turnoverratio': 0}, {'symbol': 'sh688169', 'code': '688169', 'name': '石头科技', 'trade': '756.000', 'pricechange': 0, 'changepercent': 0, 'buy': '756.000', 'sell': '756.000', 'settlement': '756.000', 'open': '0.000', 'high': '0.000', 'low': '0.000', 'volume': 0, 'amount': 0, 'ticktime': '09:16:53', 'per': 35.278, 'pb': 6.253, 'mktcap': 5049356.13, 'nmc': 3421194.84, 'turnoverratio': 0}] 爬虫爬到一半报(说明大部分数据没问题中间一两个数据格式不对)错建议加一个try:except:把报错部分摘一个log写到txt里 小垃圾一笑 发表于 2021-12-2 10:07
爬虫爬到一半报(说明大部分数据没问题中间一两个数据格式不对)错建议加一个try:except:把报错部分摘一 ...
这个怎样处理呢 爬取不到json了所以报错,一般是网页结构变了,或者请求的数据已经完结了引起的。
把代码贴全,帮你看看。 suchocolate 发表于 2021-12-2 11:33
爬取不到json了所以报错,一般是网页结构变了,或者请求的数据已经完结了引起的。
把代码贴全,帮你看看。
def get_gplist(list_id,list_name):
gg_url = 'http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData'
# 取JSON数据,类型为list,注意timeout时间太短会出错
gg_json = requests.get(url=gg_url, params=get_parameter(list_id), headers=get_request_headers(), timeout=(3.05, 27))
gg_data = gg_json.json() # 转换成字典
print(type(gg_data))
# 遍历字典取代码和名称
for data in gg_data:
print(data['code'], data['name'])
gg_json.close() # 关闭WEB请求response
return gg_data molinchz 发表于 2021-12-2 11:48
def get_gplist(list_id,list_name):
gg_url = 'http://vip.stock.finance.sina.com.cn/quotes_serv ...
代码贴全,就一个函数看不出你传入了什么,也看不出调用的其他函数具体的工作。 本帖最后由 molinchz 于 2021-12-3 13:18 编辑
suchocolate 发表于 2021-12-2 12:10
代码贴全,就一个函数看不出你传入了什么,也看不出调用的其他函数具体的工作。
我把整个传上来 不知是不是JSOn的数据不能单引号,要处理成双引号。 molinchz 发表于 2021-12-2 17:00
我把整个传上来
测试了一下,没啥问题,估计是被反爬机制限制了. z5560636 发表于 2021-12-3 10:12
测试了一下,没啥问题,估计是被反爬机制限制了.
反爬机制是那个
molinchz 发表于 2021-12-3 10:17
反爬机制是那个
chgn_730458,碳基材料,第16个下载完成
[{'symbol': 'sh600061', 'code': '600061', 'name': '国投资本', 'trade': '8.150', 'pricechange': -0.03, 'changepercent': -0.367, 'buy': '8.150', 'sell': '8.160', 'settlement': '8.180', 'open': '8.200', 'high': '8.250', 'low': '8.150', 'volume': 5959791, 'amount': 48835079, 'ticktime': '10:19:15', 'per': 8.402, 'pb': 1.155, 'mktcap': 5236619.067235, 'nmc': 5236619.067235, 'turnoverratio': 0.09276}, {'symbol': 'sh600100', 'code': '600100', 'name': '同方股份', 'trade': '5.560', 'pricechange': -0.02, 'changepercent': -0.358, 'buy': '5.550', 'sell': '5.560', 'settlement': '5.580', 'open': '5.590', 'high': '5.610', 'low': '5.550', 'volume': 3883400, 'amount': 21625413, 'ticktime': '10:19:15', 'per': 160.694, 'pb': 1.054, 'mktcap': 1862765.528428, 'nmc': 1647927.816756, 'turnoverratio': 0.13102}, {'symbol': 'sh600266', 'code': '600266', 'name': '城建发展', 'trade': '4.290', 'pricechange': 0, 'changepercent': 0, 'buy': '4.280', 'sell': '4.290', 'settlement': '4.290', 'open': '4.280', 'high': '4.300', 'low': '4.270', 'volume': 1558706, 'amount': 6681440, 'ticktime': '10:19:12', 'per': 9.662, 'pb': 0.449, 'mktcap': 968054.6304, 'nmc': 968054.6304, 'turnoverratio': 0.06908}, {'symbol': 'sh600282', 'code': '600282', 'name': '南钢股份', 'trade': '3.570', 'pricechange': -0.03, 'changepercent': -0.833, 'buy': '3.560', 'sell': '3.570', 'settlement': '3.600', 'open': '3.610', 'high': '3.620', 'low': '3.560', 'volume': 11865670, 'amount': 42536593, 'ticktime': '10:19:15', 'per': 6.432, 'pb': 0.858, 'mktcap': 2198667.581397, 'nmc': 1592423.114436, 'turnoverratio': 0.26601}, {'symbol': 'sh600596', 'code': '600596', 'name': '新安股份', 'trade': '26.220', 'pricechange': -0.16, 'changepercent': -0.607, 'buy': '26.210', 'sell': '26.220', 'settlement': '26.380', 'open': '26.330', 'high': '26.380', 'low': '25.840', 'volume': 8908912, 'amount': 232431766, 'ticktime': '10:19:15', 'per': 35.058, 'pb': 2.71, 'mktcap': 2145819.592092, 'nmc': 1927669.192092, 'turnoverratio': 1.21178}, {'symbol': 'sh600658', 'code': '600658', 'name': '电子城', 'trade': '3.880', 'pricechange': -0.01, 'changepercent': -0.257, 'buy': '3.870', 'sell': '3.880', 'settlement': '3.890', 'open': '3.900', 'high': '3.910', 'low': '3.870', 'volume': 964400, 'amount': 3748575, 'ticktime': '10:19:15', 'per': 8.255, 'pb': 0.66, 'mktcap': 434010.99746, 'nmc': 434010.99746, 'turnoverratio': 0.08622}, {'symbol': 'sh600683', 'code': '600683', 'name': '京投发展', 'trade': '5.490', 'pricechange': -0.15, 'changepercent': -2.66, 'buy': '5.480', 'sell': '5.490', 'settlement': '5.640', 'open': '5.610', 'high': '5.630', 'low': '5.450', 'volume': 8599500, 'amount': 47378132, 'ticktime': '10:19:15', 'per': 8.194, 'pb': 1.347, 'mktcap': 406686.900753, 'nmc': 406686.900753, 'turnoverratio': 1.16087}, {'symbol': 'sh601066', 'code': '601066', 'name': '中信建投', 'trade': '28.530', 'pricechange': 0.12, 'changepercent': 0.422, 'buy': '28.530', 'sell': '28.550', 'settlement': '28.410', 'open': '28.520', 'high': '28.890', 'low': '28.440', 'volume': 7096295, 'amount': 203616148, 'ticktime': '10:19:15', 'per': 23.775, 'pb': 3.576, 'mktcap': 22129850.255841, 'nmc': 10873815.837354, 'turnoverratio': 0.18619}, {'symbol': 'sh601198', 'code': '601198', 'name': '东兴证券', 'trade': '12.040', 'pricechange': -0.16, 'changepercent': -1.311, 'buy': '12.040', 'sell': '12.060', 'settlement': '12.200', 'open': '12.200', 'high': '12.290', 'low': '12.040', 'volume': 8486531, 'amount': 103356206, 'ticktime': '10:19:15', 'per': 21.577, 'pb': 1.518, 'mktcap': 3891864.40608, 'nmc': 3320584.631028, 'turnoverratio': 0.30771}, {'symbol': 'sh601881', 'code': '601881', 'name': '中国银河', 'trade': '10.530', 'pricechange': 0, 'changepercent': 0, 'buy': '10.530', 'sell': '10.540', 'settlement': '10.530', 'open': '10.560', 'high': '10.640', 'low': '10.530', 'volume': 5902275, 'amount': 62490570, 'ticktime': '10:19:15', 'per': 14.831, 'pb': 1.314, 'mktcap': 10674533.471121, 'nmc': 6787926.652572, 'turnoverratio': 0.09156}, {'symbol': 'sh601995', 'code': '601995', 'name': '中金公司', 'trade': '49.980', 'pricechange': 0.23, 'changepercent': 0.462, 'buy': '49.980', 'sell': '49.990', 'settlement': '49.750', 'open': '49.750', 'high': '50.970', 'low': '49.610', 'volume': 8643484, 'amount': 435242249, 'ticktime': '10:19:15', 'per': 31.277, 'pb': 3.313, 'mktcap': 24126629.826264, 'nmc': 2654544.162438, 'turnoverratio': 1.6274}, {'symbol': 'sh603566', 'code': '603566', 'name': '普莱柯', 'trade': '18.620', 'pricechange': 0.04, 'changepercent': 0.215, 'buy': '18.620', 'sell': '18.630', 'settlement': '18.580', 'open': '18.580', 'high': '18.760', 'low': '18.500', 'volume': 495100, 'amount': 9214163, 'ticktime': '10:19:16', 'per': 25.861, 'pb': 3.357, 'mktcap': 598625.552, 'nmc': 598625.552, 'turnoverratio': 0.154}, {'symbol': 'sz000551', 'code': '000551', 'name': '创元科技', 'trade': '10.640', 'pricechange': -0.25, 'changepercent': -2.296, 'buy': '10.640', 'sell': '10.650', 'settlement': '10.890', 'open': '10.870', 'high': '11.110', 'low': '10.600', 'volume': 6750100, 'amount': 73422320, 'ticktime': '10:19:15', 'per': 36.69, 'pb': 2.141, 'mktcap': 425685.55092, 'nmc': 425685.55092, 'turnoverratio': 1.68719}, {'symbol': 'sz000666', 'code': '000666', 'name': '经纬纺机', 'trade': '8.340', 'pricechange': 0.09, 'changepercent': 1.091, 'buy': '8.340', 'sell': '8.350', 'settlement': '8.250', 'open': '8.350', 'high': '8.460', 'low': '8.280', 'volume': 1819859, 'amount': 15260882, 'ticktime': '10:19:12', 'per': 14.305, 'pb': 0.621, 'mktcap': 587244.42, 'nmc': 244815.356562, 'turnoverratio': 0.61996}, {'symbol': 'sz000777', 'code': '000777', 'name': '中核科技', 'trade': '15.850', 'pricechange': 0.11, 'changepercent': 0.699, 'buy': '15.830', 'sell': '15.850', 'settlement': '15.740', 'open': '15.730', 'high': '16.070', 'low': '15.610', 'volume': 4067095, 'amount': 64676831, 'ticktime': '10:19:15', 'per': 58.08, 'pb': 3.871, 'mktcap': 612757.184905, 'nmc': 607699.383335, 'turnoverratio': 1.06078}, {'symbol': 'sz000917', 'code': '000917', 'name': '电广传媒', 'trade': '5.950', 'pricechange': 0.02, 'changepercent': 0.337, 'buy': '5.940', 'sell': '5.950', 'settlement': '5.930', 'open': '5.960', 'high': '5.970', 'low': '5.920', 'volume': 3030069, 'amount': 17993340, 'ticktime': '10:19:15', 'per': -5.721, 'pb': 0.812, 'mktcap': 843446.02111, 'nmc': 843291.62694, 'turnoverratio': 0.21379}, {'symbol': 'sz000931', 'code': '000931', 'name': '中 关 村', 'trade': '6.890', 'pricechange': -0.12, 'changepercent': -1.712, 'buy': '6.890', 'sell': '6.900', 'settlement': '7.010', 'open': '7.000', 'high': '7.030', 'low': '6.890', 'volume': 4075428, 'amount': 28336273, 'ticktime': '10:19:15', 'per': 203.846, 'pb': 3.146, 'mktcap': 518904.490598, 'nmc': 514126.275598, 'turnoverratio': 0.54616}, {'symbol': 'sz002004', 'code': '002004', 'name': '华邦健康', 'trade': '7.380', 'pricechange': 0.03, 'changepercent': 0.408, 'buy': '7.370', 'sell': '7.380', 'settlement': '7.350', 'open': '7.320', 'high': '7.450', 'low': '7.210', 'volume': 18672293, 'amount': 136683038, 'ticktime': '10:19:15', 'per': 22.364, 'pb': 1.483, 'mktcap': 1461180.362958, 'nmc': 1351567.663524, 'turnoverratio': 1.01957}, {'symbol': 'sz002179', 'code': '002179', 'name': '中航光电', 'trade': '100.130', 'pricechange': 0, 'changepercent': 0, 'buy': '100.000', 'sell': '100.130', 'settlement': '100.130', 'open': '100.000', 'high': '101.500', 'low': '99.510', 'volume': 1417865, 'amount': 142843077, 'ticktime': '10:19:15', 'per': 74.418, 'pb': 9.907, 'mktcap': 11013749.815689, 'nmc': 10693749.415267, 'turnoverratio': 0.13276}, {'symbol': 'sz002215', 'code': '002215', 'name': '诺 普 信', 'trade': '5.880', 'pricechange': -0.04, 'changepercent': -0.676, 'buy': '5.880', 'sell': '5.890', 'settlement': '5.920', 'open': '5.930', 'high': '5.940', 'low': '5.860', 'volume': 2616060, 'amount': 15397512, 'ticktime': '10:19:15', 'per': 31.681, 'pb': 2.026, 'mktcap': 537288.753792, 'nmc': 419591.609136, 'turnoverratio': 0.3666}, {'symbol': 'sz002349', 'code': '002349', 'name': '精华制药', 'trade': '5.710', 'pricechange': -0.01, 'changepercent': -0.175, 'buy': '5.700', 'sell': '5.710', 'settlement': '5.720', 'open': '5.700', 'high': '5.740', 'low': '5.660', 'volume': 2542300, 'amount': 14477358, 'ticktime': '10:19:15', 'per': 41.893, 'pb': 2.164, 'mktcap': 477198.617554, 'nmc': 475067.518221, 'turnoverratio': 0.30557}, {'symbol': 'sz002385', 'code': '002385', 'name': '大北农', 'trade': '9.190', 'pricechange': 0.22, 'changepercent': 2.453, 'buy': '9.180', 'sell': '9.190', 'settlement': '8.970', 'open': '8.950', 'high': '9.200', 'low': '8.920', 'volume': 42918705, 'amount': 390748223, 'ticktime': '10:19:15', 'per': 19.146, 'pb': 3.38, 'mktcap': 3805838.022907, 'nmc': 2843616.192039, 'turnoverratio': 1.38705}, {'symbol': 'sz002491', 'code': '002491', 'name': '通鼎互联', 'trade': '6.220', 'pricechange': -0.05, 'changepercent': -0.797, 'buy': '6.210', 'sell': '6.220', 'settlement': '6.270', 'open': '6.210', 'high': '6.290', 'low': '6.080', 'volume': 15896791, 'amount': 98115418, 'ticktime': '10:19:15', 'per': 179.251, 'pb': 2.734, 'mktcap': 777788.04198, 'nmc': 738392.696062, 'turnoverratio': 1.3391}, {'symbol': 'sz002827', 'code': '002827', 'name': '高争民爆', 'trade': '9.990', 'pricechange': -0.05, 'changepercent': -0.498, 'buy': '9.990', 'sell': '10.000', 'settlement': '10.040', 'open': '10.080', 'high': '10.090', 'low': '9.980', 'volume': 341750, 'amount': 3422972, 'ticktime': '10:19:06', 'per': 76.846, 'pb': 3.685, 'mktcap': 275724, 'nmc': 275724, 'turnoverratio': 0.12382}, {'symbol': 'sz300011', 'code': '300011', 'name': '鼎汉技术', 'trade': '7.230', 'pricechange': -0.13, 'changepercent': -1.766, 'buy': '7.220', 'sell': '7.230', 'settlement': '7.360', 'open': '7.330', 'high': '7.390', 'low': '7.220', 'volume': 4909200, 'amount': 35727382, 'ticktime': '10:19:15', 'per': -8.6, 'pb': 2.806, 'mktcap': 403904.229801, 'nmc': 362072.616723, 'turnoverratio': 0.98029}, {'symbol': 'sz300688', 'code': '300688', 'name': '创业黑马', 'trade': '37.750', 'pricechange': -2.85, 'changepercent': -7.02, 'buy': '37.750', 'sell': '37.760', 'settlement': '40.600', 'open': '39.830', 'high': '40.200', 'low': '37.370', 'volume': 7677040, 'amount': 295573626, 'ticktime': '10:19:15', 'per': 471.875, 'pb': 6.999, 'mktcap': 412263.8844, 'nmc': 332677.095825, 'turnoverratio': 8.7114}, {'symbol': 'sz300691', 'code': '300691', 'name': '联合光电', 'trade': '22.920', 'pricechange': 0.87, 'changepercent': 3.946, 'buy': '22.920', 'sell': '22.930', 'settlement': '22.050', 'open': '22.330', 'high': '23.310', 'low': '22.330', 'volume': 6648000, 'amount': 151756538, 'ticktime': '10:19:15', 'per': 104.182, 'pb': 5.118, 'mktcap': 516984.931872, 'nmc': 419569.275216, 'turnoverratio': 3.63163}]
600061 国投资本
600100 同方股份
600266 城建发展
600282 南钢股份
600596 新安股份
600658 电子城
600683 京投发展
601066 中信建投
601198 东兴证券
601881 中国银河
601995 中金公司
603566 普莱柯
000551 创元科技
000666 经纬纺机
000777 中核科技
000917 电广传媒
000931 中 关 村
002004 华邦健康
002179 中航光电
002215 诺 普 信
002349 精华制药
002385 大北农
002491 通鼎互联
002827 高争民爆
300011 鼎汉技术
300688 创业黑马
300691 联合光电
chgn_701021,北交所,第17个下载完成
就是不能不停的运行,你要等个10几分钟再运行 是全部下完再等10分钟在下 是不 z5560636 发表于 2021-12-3 10:20
就是不能不停的运行,你要等个10几分钟再运行
是全部下完再等10分钟在下 是不 molinchz 发表于 2021-12-3 10:40
是全部下完再等10分钟在下 是不
是的
思想:服务器后台对访问的User_Agent进行统计,单位时间内同一User_Agent访问的次数超过特定的阀值,则会被不同程度的封禁IP,从而造成无法进行爬虫的状况。 z5560636 发表于 2021-12-3 14:18
是的
大神,从那里可看出这个限制,学习下
页:
[1]