鱼C论坛

 找回密码
 立即注册
查看: 2184|回复: 6

[已解决]正则表达式问题

[复制链接]
发表于 2021-4-6 13:08:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pat = r'"Data":[["(.*?)"]]'
status = re.findall(pat,html)
这是我写的正则表达式,图片是要提取的数据
想请问大佬错在哪了
最佳答案
2021-4-6 14:16:23
本帖最后由 591821661 于 2021-4-6 14:26 编辑

最好用json.loads解析
屏幕截图 2021-04-06 130627.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-6 13:37:19 | 显示全部楼层
把原数据也传上来,图片数据看起来不全。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-6 13:45:41 | 显示全部楼层
suchocolate 发表于 2021-4-6 13:37
把原数据也传上来,图片数据看起来不全。

import re
from bs4 import BeautifulSoup
import requests
import urllib.request
import json

def main():
    baseurl = "http://homeflashdata2.jrj.com.cn/limitStatistic/ztForce/20210402.js"
    datalist = getdata(baseurl)
    temp = input("请输入股票日期:")

def getdata(baseurl):
    datalist =[]
    html = askurl(baseurl)
    #soup = BeautifulSoup(html,"html.parser")
    pat = '^"Data":\[\["(.*?)"\]\]'
    status = re.findall(pat,html)
    print(status)





def askurl(baseurl):
    head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"}
    req = requests.get(baseurl,headers = head)
    response = req.text
    html = json.loads(response)


    #req = urllib.request.Request(baseurl,headers=head)
    # response = urllib.request.urlopen(req)
    # html = response.read().decode('utf-8')
    #print(html)

    return html

def savedata():
    savedata = []


if __name__ == '__main__':
    #main()
    askurl("http://stock.jrj.com.cn/tzzs/zdtwdj/zdforce.shtml")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-6 13:48:08 | 显示全部楼层
傲视群熊¥ 发表于 2021-4-6 13:45
import re
from bs4 import BeautifulSoup
import requests

不好意思,刚刚发错了。以下才是:
"D:\PyCharm Community Edition 2020.3.3\venv\Scripts\python.exe" D:/股票/main.py
var yzb_ztForce={"summary":{size:89,time:"2021-04-06 13:47:25"},
"column":{stockcode:0,stockname:1,nowPrice:2,priceLimit:3,fcb:4,flb:5,fdMoney:6,firstZtTime:7,lastZtTime:8,opentime:9,zhenfu:10,force : 11},
"Data":[["000862","银星能源",6.39,9.98,15.231798,0.11174704,3.57995776E8,"09:25:00","09:25:00",0,0.0,100.0]
,["002762","金发拉比",7.37,10.0,39.256596,0.21213804,3.011688E8,"09:25:00","09:25:00",0,0.0,100.0]
,["003040","楚天龙",17.28,9.99,82.339676,0.57713294,7.8177235E8,"09:25:00","09:25:00",0,0.0,100.0]
,["003041","N真爱",25.92,44.0,38.92834,0.3016233,1.95451904E8,"09:25:00","09:25:00",0,24.0,100.0]
,["003042","N中农",31.05,44.02,59.7738,0.41554022,3.53529152E8,"09:25:00","09:25:00",0,24.03,100.0]
,["003043","N华亚",28.53,44.02,67.50838,0.48461854,2.7652336E8,"09:25:00","09:25:00",0,24.03,100.0]
,["603555","*ST贵人",2.74,4.98,10.863232,0.005433344,9358196.0,"09:25:04","09:25:04",0,0.0,100.0]
,["603759","海天股份",28.59,10.0,17.06961,0.21136008,4.71337216E8,"09:25:03","09:25:03",0,0.0,100.0]
,["600666","ST瑞德",1.7,4.94,6.1612706,0.012573849,1.717867E7,"09:25:02","09:25:02",0,0.0,86.564445]
,["003026","中晶科技",89.09,10.0,6.1455545,0.11478517,2.55143952E8,"09:25:00","09:25:00",0,0.0,86.509445]
,["002219","*ST恒康",3.01,4.88,1.5780689,0.0055152164,3.0613204E7,"09:30:00","09:30:00",0,1.05,84.75826]
,["600702","ST舍得",91.99,5.0,2.986,0.005998715,1.82939952E8,"09:25:05","09:25:05",0,0.0,75.451004]
,["600306","*ST商城",7.15,4.99,1.365803,0.0043979716,5580317.5,"09:36:54","09:30:00",1,0.15,73.53002]
,["002733","雄韬股份",16.65,9.97,1.4523677,0.017813077,1.08340528E8,"09:32:57","09:32:57",0,7.07,72.54274]
,["600715","文投控股",2.59,10.21,1.9957778,0.007977302,3.8323452E7,"09:25:03","09:25:03",0,0.0,71.98522]
,["002542","中化岩土",3.96,10.0,1.5121642,0.029130584,1.67798448E8,"09:25:00","09:25:00",0,0.0,70.29257]
,["002618","丹邦科技",5.15,10.04,1.4133043,0.02378974,6.71296E7,"09:25:00","09:25:00",0,0.0,69.94656]
,["003036","泰坦股份",19.8,10.0,0.3301888,0.051216666,5.4760856E7,"09:32:48","09:32:48",0,4.0,69.401886]
,["605208","永茂泰",31.85,10.02,0.29759592,0.038575914,5.7746216E7,"09:32:13","09:32:13",0,3.97,69.19884]
,["003039","顺控发展",56.83,10.01,0.37223652,0.041464597,1.46098848E8,"09:38:03","09:38:03",0,4.99,68.93969]
,["603988","中电电机",22.11,10.0,0.29909918,0.02479747,1.28953592E8,"09:32:13","09:32:13",0,4.63,68.48498]
,["603105","芯能科技",9.02,10.0,1.3348571,0.03765538,1.04714568E8,"09:39:56","09:39:56",0,10.61,67.92685]
,["002971","和远气体",20.21,10.02,1.2472578,0.035388555,7.6540984E7,"10:16:21","10:16:21",0,9.8,67.84952]
,["000820","*ST节能",2.17,4.83,0.3537382,0.005430717,3389974.0,"09:33:09","09:32:45",2,0.97,67.594536]
,["600509","天富能源",8.62,9.95,0.32113957,0.019109186,1.89663264E8,"09:25:01","09:25:01",0,3.44,64.86694]
,["600518","ST康美",2.16,4.85,0.4130634,0.0015815224,1.5060817E7,"09:41:10","09:41:10",0,4.85,64.28854]
,["001896","豫能控股",9.34,10.01,0.27028328,0.019106044,2.05323296E8,"09:36:54","09:25:00",7,3.65,64.14836]
,["603055","台华新材",6.3,9.95,0.94669926,0.0061396984,2.994768E7,"10:20:34","10:20:34",0,10.82,63.677692]
,["600877","ST电能",13.7,4.98,0.35374838,0.0032560239,3.065786E7,"10:43:46","10:43:46",0,5.59,62.963764]
,["000408","*ST藏格",10.65,5.03,0.2618117,0.0038830135,1.9580748E7,"10:02:48","10:02:48",0,5.13,62.50753]
,["600215","*ST经开",6.34,4.97,0.52814996,0.0020593016,6071418.5,"09:37:34","09:33:19",5,3.15,62.141796]
,["002564","天沃科技",3.69,10.15,1.1737792,0.034179863,1.09516984E8,"09:47:33","09:46:36",1,10.15,61.75869]
,["000723","美锦能源",7.54,10.07,0.45669767,0.0115471445,3.2859376E8,"09:52:00","09:52:00",0,7.88,61.744713]
,["002289","ST宇顺",6.91,5.02,0.11487316,0.0029196888,5340739.0,"10:19:33","09:39:15",23,4.56,61.603333]
,["002636","金安国纪",12.49,10.04,0.83630097,0.01466914,1.32486448E8,"09:36:33","09:32:57",1,7.14,61.296356]
,["601798","蓝科高新",7.45,10.04,0.59826833,0.018623812,4.9190112E7,"10:55:44","10:55:44",0,10.04,61.035805]
,["600458","时代新材",8.76,10.05,0.5569134,0.019610805,1.13625776E8,"11:05:52","11:05:52",0,10.3,60.367188]
,["600248","陕西建工",4.77,9.91,0.65412676,0.030457009,9.5402424E7,"13:00:45","13:00:45",0,11.75,59.943554]
,["600576","祥源文化",5.48,10.04,0.72034967,0.020931548,7.1048208E7,"09:50:42","09:50:42",0,12.65,59.658154]
,["300405","科隆股份",8.53,19.97,0.38731483,0.048242386,6.0405192E7,"09:41:51","09:41:51",0,9.99,59.12436]
,["600122","*ST宏图",1.24,5.08,0.4039589,0.0038202987,5486875.0,"09:34:25","09:34:01",2,5.08,58.97869]
,["000881","中广核技",13.89,9.98,0.47603852,0.016902171,1.64457184E8,"10:27:15","10:27:15",0,11.01,58.834896]
,["603112","华翔股份",15.58,10.03,0.0629537,0.041305225,3.4236084E7,"13:32:10","09:25:04",7,8.05,57.786064]
,["002901","大博医疗",57.49,10.01,0.21149506,0.0016220042,3.1702804E7,"10:31:12","10:31:12",0,9.36,57.76306]
,["300931","通用电梯",17.05,19.99,0.21441548,0.07492086,7.2735192E7,"10:07:30","10:07:30",0,9.64,57.604156]
,["002138","顺络电子",34.82,10.02,0.06332239,0.001503247,3.7077728E7,"10:55:48","09:35:39",28,3.06,57.580738]
,["603157","*ST拉夏",2.13,4.93,0.27764446,0.008065844,2504880.2,"10:13:33","09:38:54",18,5.42,57.396774]
,["002354","*ST天娱",3.15,5.0,0.4026446,0.0021625373,1.0172484E7,"11:17:03","10:56:18",5,6.67,57.367256]
,["002147","*ST新光",1.67,5.03,0.25188404,0.005683985,5097341.0,"10:34:45","09:59:09",32,5.66,56.88726]
,["002740","爱迪尔",4.91,10.09,0.18622811,0.0085345,1.0127454E7,"09:46:24","09:46:24",0,10.09,56.814953]
,["603933","睿能科技",10.84,10.05,0.6199223,0.010633673,2.3195584E7,"09:53:49","09:51:49",2,9.54,56.71239]
,["002329","皇氏集团",4.49,10.05,0.72591805,0.02757979,6.601922E7,"11:29:48","11:28:42",1,10.78,56.61708]
,["002280","*ST联络",3.31,5.08,0.064430945,0.0023555178,1.3732362E7,"10:24:30","09:25:00",55,4.13,56.526085]
,["603693","江苏新能",17.3,9.98,0.13037328,0.035271097,1.17766664E8,"11:29:58","09:33:12",2,9.98,56.500088]
,["002501","*ST利源",3.45,4.86,0.073450096,4.836337E-4,5790221.5,"10:50:36","09:55:12",34,4.26,56.476917]
,["002630","华西能源",2.94,10.11,0.2987938,0.033438325,9.5512784E7,"11:05:48","11:05:48",0,11.99,56.16513]
,["600870","ST厦华",3.32,5.06,0.023470646,2.5133791E-4,436580.0,"13:45:23","09:40:54",9,4.43,55.80596]
,["600956","新天绿能",14.14,10.04,0.2989187,0.08976618,1.71037344E8,"09:35:22","09:34:31",1,7.32,55.769188]
,["002306","ST云网",3.05,5.17,0.009035086,1.4792303E-4,357765.0,"13:45:51","09:50:18",11,4.48,55.61109]
,["002089","ST新海",1.76,4.76,0.024664544,2.2584628E-4,469920.0,"13:27:36","13:00:51",11,5.36,54.887775]
,["600721","*ST百花",3.55,5.03,0.010192058,9.589535E-5,122830.0,"13:00:08","11:06:07",5,5.33,54.7724]
,["000966","长源电力",8.38,9.97,0.35237014,0.050001882,4.64386816E8,"13:03:51","13:03:51",0,14.04,54.583702]
,["002535","*ST林重",1.97,4.79,0.120780714,0.004851309,5440844.5,"13:23:06","13:00:39",24,6.91,54.322063]
,["003009","中天火箭",53.97,10.01,0.31119677,0.03751292,7.865464E7,"10:25:33","09:38:36",2,9.01,54.28953]
,["600218","全柴动力",9.71,9.97,0.21137488,0.016267238,5.8247444E7,"10:06:17","09:54:56",1,8.61,53.585087]
,["603839","安正时尚",10.18,10.05,0.25075868,0.009655456,3.9326848E7,"10:03:11","09:51:50",8,9.51,53.045864]
,["002356","*ST赫美",1.63,5.16,0.048096683,9.958785E-4,854657.9,"13:44:48","10:33:09",45,7.74,52.745945]
,["603819","神力股份",10.33,10.01,0.2282723,0.004742121,1.0647131E7,"09:52:27","09:47:24",3,9.9,52.406433]
,["603698","航天工程",16.43,9.97,0.13006979,0.0035645366,3.1390436E7,"10:44:33","10:43:24",1,8.97,52.34852]
,["002002","鸿达兴业",3.85,10.0,0.16867308,0.008859541,9.7706912E7,"09:55:27","09:40:24",3,9.43,52.30103]
,["000009","中国宝安",10.08,10.04,0.101411045,0.0030158618,7.7563184E7,"10:13:30","09:56:30",26,8.95,52.07919]
,["002665","首航高科",2.6,10.17,0.15934172,0.016904624,1.02505776E8,"11:12:54","10:22:27",11,10.17,51.507942]
,["002619","艾格拉斯",1.85,10.12,0.21246673,0.014193299,3.7584344E7,"11:10:57","10:42:21",4,10.71,51.485634]
,["000948","南天信息",11.75,10.02,0.1909361,0.0129293,4.8682528E7,"09:56:42","09:37:48",3,10.49,51.48401]
,["000566","海南海药",6.39,9.98,0.22724116,0.0068898294,5.1307468E7,"13:13:48","10:20:54",3,10.84,51.46686]
,["605299","舒华体育",14.22,9.98,0.2593005,0.0809004,5.7520184E7,"11:25:04","10:59:28",5,11.91,50.783005]
,["002996","顺博合金",23.98,10.0,0.08023005,0.033890568,4.3072876E7,"10:30:24","10:08:45",9,10.23,50.741753]
,["002610","爱康科技",2.78,9.88,0.008708434,6.0300215E-4,7352822.0,"13:46:30","11:08:54",32,9.49,50.600098]
,["002161","远 望 谷",5.2,9.94,0.2591233,0.010767871,3.988712E7,"11:27:06","10:47:15",3,12.05,50.59507]
,["002174","游族网络",13.29,10.02,0.15731816,0.008229529,7.5443728E7,"10:11:09","09:56:54",8,11.09,50.52433]
,["002272","川润股份",5.97,9.94,0.0429673,0.0073067057,1.4285015E7,"13:45:36","11:15:42",21,10.13,50.336205]
,["002549","凯美特气",10.21,10.02,0.13720182,0.010368336,6.5657988E7,"10:14:00","09:43:00",8,11.85,49.57386]
,["000620","新华联",2.83,10.12,0.15807177,0.0054345294,2.9162866E7,"10:02:27","09:52:48",1,12.84,48.76789]
,["300098","高新兴",3.76,20.13,0.3110139,0.02330018,1.20843032E8,"13:12:48","13:12:48",0,19.49,48.73664]
,["300471","厚普股份",10.93,19.98,0.20828423,0.020251757,5.3412168E7,"09:44:00","09:42:27",1,13.94,48.244102]
,["600917","重庆燃气",12.78,9.98,0.10652587,0.0026390103,5.2478512E7,"09:37:58","09:37:07",2,13.94,47.138454]
,["002639","雪人股份",6.58,10.03,1.6557963,0.036701348,1.34915936E8,"","",0,4.68,0.0]
,["600379","宝光股份",9.89,10.01,1.6959394,0.041126832,1.34306992E8,"","",0,2.0,0.0]
,["600983","惠而浦",8.93,9.98,0.76599437,0.0038062993,2.605149E7,"","",0,4.8,0.0]
]};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 14:16:23 | 显示全部楼层    本楼为最佳答案   
本帖最后由 591821661 于 2021-4-6 14:26 编辑

最好用json.loads解析
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-6 14:41:37 | 显示全部楼层
591821661 发表于 2021-4-6 14:16
最好用json.loads解析

但是我用json它会给我报错:
"D:\PyCharm Community Edition 2020.3.3\venv\Scripts\python.exe" D:/股票/main.py
Traceback (most recent call last):
  File "D:/股票/main.py", line 61, in <module>
    askurl("http://homeflashdata2.jrj.com.cn/limitStatistic/ztForce/20210406.js?_=1617685567167")
  File "D:/股票/main.py", line 45, in askurl
    html = json.loads(response)
  File "C:\Users\CJS\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\CJS\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\CJS\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 18:01:54 | 显示全部楼层
pat = r'"Data":[["(.*?)"]]'

注意看红色的地方,错误应该是在这了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-22 16:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表