正则(re.findall) 抓取script中的数据

以某网站为例(政府性质),路由地址就不发了

1、网站数据如下

在这里插入图片描述
数据是动态加载,无法使用xpath标签解析

在这里插入图片描述

2、re.findall()抓取
import re
import json
import requests


# 获取详情页
def spider_test(pageid):
    url = "http://xxxxxxxxxxxxx/view?id=%s" % pageid

    headers = {
        'Cookie': 'HttpOnly; JSESSIONID=F24B4AB94E907005C1D88A8E6991EBCE'
    }

    response = requests.request("GET", url, headers=headers, data={}).text

    # enterprise :ko.mapping.fromJS 定位到方法
    # \(\) 转义 以"("开始匹配,")"结束 .*?非贪婪匹配 精准抓取目标数据
    # re.S 忽略换行符(\n) 抓取范围内的所有字符
    content = re.findall(r'enterprise :ko.mapping.fromJS\((.*?)\),', response, re.S)
    print("content:", content)
    content = json.loads(content[0])
    print("content:", content)
    print(content.get("fcEntname"))


if __name__ == '__main__':
    spider_test("2c9297bd7bd453c5017bde00066d08c7")

#content: ['{\n  "id" : "2c9297bd7bd453c5017bde00066d08c7",\n  "fcEntcode" : "GGZY20489",\n  "fcEntname" : "东莞市黄江镇胜前岗股份经济联合社",\n  "fcArtname" : "叶亮华",\n  "fnArtpapertype" : "1",\n  "fcArtpapersn" : "441900198101074710",\n  "fcCountry" : null,\n  "fcBelongareasn" : "441900",\n  "fcBelongareasnName" : "东莞市",\n  "fcEntpropertysn" : null,\n  "fcIndustrysn" : null,\n  "fcBusinesslicenseno" : "N2441900725475951U",\n  "fcRegistrationno" : "441900725457951",\n  "fdBuilddate" : "2004-09-20",\n  "fdBusinessauthority" : "东莞市农业农村局",\n  "fdVaildenddate" : "2029-09-24",\n  "fcCreditgrade" : null,\n  "fmEnrolfund" : 0,\n  "fcCurrencysn" : null,\n  "fcEnrolbaseunit" : null,\n  "fcEntinfodeclareperson" : "黎小姐",\n  "fdEntinfodeclaredate" : null,\n  "fcEntinfodeclarepersontel" : "13712881148",\n  "fcEntlinktel" : "0769-83361231",\n  "fcCaid" : null,\n  "fcEntlinkaddr" : "广东省东莞市黄江镇田美北社区胜前岗村江北路胜前岗花园七街6号201",\n  "fcEntlinkzipcode" : "                  ",\n  "fcEntlinkemail" : "275160992@qq.com",\n  "fcRoletypesn" : null,\n  "fnEntstatus" : "1",\n  "fcFlowstatus" : "3",\n  "fcAccount" : "2c9297bd7bd453c5017bde00066d08c5",\n  "fdCreatetime" : "2021-09-13 15:12:28",\n  "fcCreator" : "yelianghua123",\n  "fdModifytime" : "2021-09-13 16:22:59",\n  "fcModifier" : "yelianghua123",\n  "fcOrganizationcode" : "N2441900725475951U",\n  "fcOrganizationauthority" : "东莞市农业农村局",\n  "fdSafelicencesdate" : "2019-09-25",\n  "fdSafelicenceedate" : "2029-09-24",\n  "fcSafelicencenumber" : "N2441900725475951U",\n  "fcSafelicencecert" : "东莞市农业农村局",\n  "fcOrganizingauthority" : null,\n  "fcOrganizingcode" : null,\n  "fcBusinesstype" : null,\n  "fcSubjectrole" : null,\n  "fcRegistrationdept" : null,\n  "fcRentregistrationno" : null,\n  "fcRentregistrationdept" : null,\n  "fcSocialno" : null,\n  "fcSocialdept" : null,\n  "fcOrganizationintro" : null,\n  "fnIsotherprovinces" : "0",\n  "fcIntogdadress" : null,\n  "fdEffectivedate" : "2021-09-15 09:06:47",\n  "fcResponsibleofficer" : null,\n  "fcIndustrycode" : null,\n  "fdAudittime" : "2021-09-15 09:06:47",\n  "fcAuditperson" : "詹航锋",\n  "fnIsswotherprovinces" : "0",\n  "fcSwintogdadress" : null,\n  "fnIsjtotherprovinces" : "0",\n  "fcJtintogdadress" : null,\n  "fcBelongtrade" : null\n}']
#content: {'id': '2c9297bd7bd453c5017bde00066d08c7', 'fcEntcode': 'GGZY20489', 'fcEntname': '东莞市黄江镇胜前岗股份经济联合社', 'fcArtname': '叶亮华', 'fnArtpapertype': '1', 'fcArtpapersn': '441900198101074710', 'fcCountry': None, 'fcBelongareasn': '441900', 'fcBelongareasnName': '东莞市', 'fcEntpropertysn': None, 'fcIndustrysn': None, 'fcBusinesslicenseno': 'N2441900725475951U', 'fcRegistrationno': '441900725457951', 'fdBuilddate': '2004-09-20', 'fdBusinessauthority': '东莞市农业农村局', 'fdVaildenddate': '2029-09-24', 'fcCreditgrade': None, 'fmEnrolfund': 0, 'fcCurrencysn': None, 'fcEnrolbaseunit': None, 'fcEntinfodeclareperson': '黎小姐', 'fdEntinfodeclaredate': None, 'fcEntinfodeclarepersontel': '13712881148', 'fcEntlinktel': '0769-83361231', 'fcCaid': None, 'fcEntlinkaddr': '广东省东莞市黄江镇田美北社区胜前岗村江北路胜前岗花园七街6号201', 'fcEntlinkzipcode': '                  ', 'fcEntlinkemail': '275160992@qq.com', 'fcRoletypesn': None, 'fnEntstatus': '1', 'fcFlowstatus': '3', 'fcAccount': '2c9297bd7bd453c5017bde00066d08c5', 'fdCreatetime': '2021-09-13 15:12:28', 'fcCreator': 'yelianghua123', 'fdModifytime': '2021-09-13 16:22:59', 'fcModifier': 'yelianghua123', 'fcOrganizationcode': 'N2441900725475951U', 'fcOrganizationauthority': '东莞市农业农村局', 'fdSafelicencesdate': '2019-09-25', 'fdSafelicenceedate': '2029-09-24', 'fcSafelicencenumber': 'N2441900725475951U', 'fcSafelicencecert': '东莞市农业农村局', 'fcOrganizingauthority': None, 'fcOrganizingcode': None, 'fcBusinesstype': None, 'fcSubjectrole': None, 'fcRegistrationdept': None, 'fcRentregistrationno': None, 'fcRentregistrationdept': None, 'fcSocialno': None, 'fcSocialdept': None, 'fcOrganizationintro': None, 'fnIsotherprovinces': '0', 'fcIntogdadress': None, 'fdEffectivedate': '2021-09-15 09:06:47', 'fcResponsibleofficer': None, 'fcIndustrycode': None, 'fdAudittime': '2021-09-15 09:06:47', 'fcAuditperson': '詹航锋', 'fnIsswotherprovinces': '0', 'fcSwintogdadress': None, 'fnIsjtotherprovinces': '0', 'fcJtintogdadress': None, 'fcBelongtrade': None}
#东莞市黄江镇胜前岗股份经济联合社

很方便的就拿到了想要的数据,效果非常赞,但实际抓取时遇到了以下问题
在这里插入图片描述
检查该详情页数据:
在这里插入图片描述
修改代码如下

# 修改匹配规则为以"({" 开始 "})"结束
content = re.findall(r'enterprise :ko.mapping.fromJS\({(.*?)}\),', response, re.S)
print("content:", content)
# content: ['\n  "id" : "2c9296bf597d9c4e0159865158763732",\n  "fcEntcode" : "GGZY02494",\n  "fcEntname" : "广州市水务科学研究所",\n  "fcArtname" : "李志威",\n  "fnArtpapertype" : "1",\n  "fcArtpapersn" : "420106196911205058",\n  "fcCountry" : null,\n  "fcBelongareasn" : "440100",\n  "fcBelongareasnName" : "广州市",\n  "fcEntpropertysn" : "110",\n  "fcIndustrysn" : null,\n  "fcBusinesslicenseno" : "124401004553512027",\n  "fcRegistrationno" : null,\n  "fdBuilddate" : "1978-04-29",\n  "fdBusinessauthority" : "广州市事业单位登记管理局",\n  "fdVaildenddate" : "2020-02-15",\n  "fcCreditgrade" : null,\n  "fmEnrolfund" : 1316,\n  "fcCurrencysn" : null,\n  "fcEnrolbaseunit" : null,\n  "fcEntinfodeclareperson" : "李敏华",\n  "fdEntinfodeclaredate" : null,\n  "fcEntinfodeclarepersontel" : "13580421982",\n  "fcEntlinktel" : "020-34289067",\n  "fcCaid" : null,\n  "fcEntlinkaddr" : "广州市海珠区前进路基立下道北12号",\n  "fcEntlinkzipcode" : "                  ",\n  "fcEntlinkemail" : "sks2000@vip.com",\n  "fcRoletypesn" : null,\n  "fnEntstatus" : "1",\n  "fcFlowstatus" : "3",\n  "fcAccount" : "2c9296bf597d9c4e015986515875372e",\n  "fdCreatetime" : "2017-01-10 10:59:55",\n  "fcCreator" : "gzsswkxyjs",\n  "fdModifytime" : "2017-01-12 16:45:41",\n  "fcModifier" : "gzsswkxyjs",\n  "fcOrganizationcode" : "124401004553512027",\n  "fcOrganizationauthority" : "中华人民共和国国家质量监督检验检疫总局",\n  "fdSafelicencesdate" : null,\n  "fdSafelicenceedate" : null,\n  "fcSafelicencenumber" : null,\n  "fcSafelicencecert" : null,\n  "fcOrganizingauthority" : null,\n  "fcOrganizingcode" : null,\n  "fcBusinesstype" : null,\n  "fcSubjectrole" : null,\n  "fcRegistrationdept" : null,\n  "fcRentregistrationno" : null,\n  "fcRentregistrationdept" : null,\n  "fcSocialno" : null,\n  "fcSocialdept" : null,\n  "fcOrganizationintro" : "<span style=\\"color:#000000;background-color:#F7FBFD;\\">广州市水务科学研究所(原广州市水利科学研究所,下称“水科所”)成立于1978年,是广州市水务局直属的事业单位,也是广州市水务系统唯一的科研机构。主要职能是:承担广州市水务科学研究、成果转化、科技咨询和科技推广与服务,推动水务行业科技进步。水科所建所以来一直致力于水务科研与新技术推得省、市科技进步奖二十多项,2009年度有3项产品获得广州市自主创新产品证书,有多项软广应用,为行业发展提供了有力的技术与支撑,先后获件获得中华人民共和国国家版权局颁发的计算机软件著作权登记证书上。1994年通过广东省技术监督局计量认证(CMA),2004年通过中国实验室认可委员会认可(CNAL),2009年通过中国国家合格评定委员会认可(CNAS), 是广东省水利行业首家通过CNAS实验室认可的检测单位,。 广州市水务科学研究所,即为广州市水务工程质量检测站,拥有一批国内领先的仪器设备和技术堪赞的资深专家及专业队伍,具备大型水利工程、市政供排水及其它建筑工程质量检测能力。始终以“公正、规范、准确、客观”为方针,以“一流技术、优质服务”为宗旨承接混凝土工程、岩土工程、地基与基础工程、金属结构、管材、量测、水量平衡等检测以及科研、咨询业务。承担广州市(含从化市、增城市)水务工程建设工程质量监督检测业务,承担广州市(含从化市、增城市)水务工程平行、对比、第三方检测业务,为水务建设提供真实、可靠的数据,取得良好的成效。 我单位具有水利工程质量检测混凝土工程、岩土工程、量测和金属结构甲级资质。水文、水资源调查评价乙级资质、测绘丙级资质、水土保持方案编制乙级资质、安防资质、水平衡测试资质。</span>",\n  "fnIsotherprovinces" : "0",\n  "fcIntogdadress" : null,\n  "fdEffectivedate" : "2017-01-13 16:57:30",\n  "fcResponsibleofficer" : null,\n  "fcIndustrycode" : null,\n  "fdAudittime" : "2017-01-13 16:57:30",\n  "fcAuditperson" : "蔡惠芬",\n  "fnIsswotherprovinces" : "1",\n  "fcSwintogdadress" : "http://gcjs.gdwater.gov.cn/article.html?type=xy&menu_first=信用信息公开&menu_second=从业单位信用信息&menu_third=企业基本信息&id=3070&tableName=Bm211xydwQyjb&name=广州市水务科学研究所(质量检测)",\n  "fnIsjtotherprovinces" : "0",\n  "fcJtintogdadress" : null,\n  "fcBelongtrade" : null\n']
# 现数据为列表,构造数据为json格式
content = str("{" + content[0] + "}")
print("content:", content)
# content: '{\n  "id" : "2c9296bf597d9c4e0159865158763732",\n  "fcEntcode" : "GGZY02494",\n  "fcEntname" : "广州市水务科学研究所",\n  "fcArtname" : "李志威",\n  "fnArtpapertype" : "1",\n  "fcArtpapersn" : "420106196911205058",\n  "fcCountry" : null,\n  "fcBelongareasn" : "440100",\n  "fcBelongareasnName" : "广州市",\n  "fcEntpropertysn" : "110",\n  "fcIndustrysn" : null,\n  "fcBusinesslicenseno" : "124401004553512027",\n  "fcRegistrationno" : null,\n  "fdBuilddate" : "1978-04-29",\n  "fdBusinessauthority" : "广州市事业单位登记管理局",\n  "fdVaildenddate" : "2020-02-15",\n  "fcCreditgrade" : null,\n  "fmEnrolfund" : 1316,\n  "fcCurrencysn" : null,\n  "fcEnrolbaseunit" : null,\n  "fcEntinfodeclareperson" : "李敏华",\n  "fdEntinfodeclaredate" : null,\n  "fcEntinfodeclarepersontel" : "13580421982",\n  "fcEntlinktel" : "020-34289067",\n  "fcCaid" : null,\n  "fcEntlinkaddr" : "广州市海珠区前进路基立下道北12号",\n  "fcEntlinkzipcode" : "                  ",\n  "fcEntlinkemail" : "sks2000@vip.com",\n  "fcRoletypesn" : null,\n  "fnEntstatus" : "1",\n  "fcFlowstatus" : "3",\n  "fcAccount" : "2c9296bf597d9c4e015986515875372e",\n  "fdCreatetime" : "2017-01-10 10:59:55",\n  "fcCreator" : "gzsswkxyjs",\n  "fdModifytime" : "2017-01-12 16:45:41",\n  "fcModifier" : "gzsswkxyjs",\n  "fcOrganizationcode" : "124401004553512027",\n  "fcOrganizationauthority" : "中华人民共和国国家质量监督检验检疫总局",\n  "fdSafelicencesdate" : null,\n  "fdSafelicenceedate" : null,\n  "fcSafelicencenumber" : null,\n  "fcSafelicencecert" : null,\n  "fcOrganizingauthority" : null,\n  "fcOrganizingcode" : null,\n  "fcBusinesstype" : null,\n  "fcSubjectrole" : null,\n  "fcRegistrationdept" : null,\n  "fcRentregistrationno" : null,\n  "fcRentregistrationdept" : null,\n  "fcSocialno" : null,\n  "fcSocialdept" : null,\n  "fcOrganizationintro" : "<span style=\\"color:#000000;background-color:#F7FBFD;\\">广州市水务科学研究所(原广州市水利科学研究所,下称“水科所”)成立于1978年,是广州市水务局直属的事业单位,也是广州市水务系统唯一的科研机构。主要职能是:承担广州市水务科学研究、成果转化、科技咨询和科技推广与服务,推动水务行业科技进步。水科所建所以来一直致力于水务科研与新技术推得省、市科技进步奖二十多项,2009年度有3项产品获得广州市自主创新产品证书,有多项软广应用,为行业发展提供了有力的技术与支撑,先后获件获得中华人民共和国国家版权局颁发的计算机软件著作权登记证书上。1994年通过广东省技术监督局计量认证(CMA),2004年通过中国实验室认可委员会认可(CNAL),2009年通过中国国家合格评定委员会认可(CNAS), 是广东省水利行业首家通过CNAS实验室认可的检测单位,。 广州市水务科学研究所,即为广州市水务工程质量检测站,拥有一批国内领先的仪器设备和技术堪赞的资深专家及专业队伍,具备大型水利工程、市政供排水及其它建筑工程质量检测能力。始终以“公正、规范、准确、客观”为方针,以“一流技术、优质服务”为宗旨承接混凝土工程、岩土工程、地基与基础工程、金属结构、管材、量测、水量平衡等检测以及科研、咨询业务。承担广州市(含从化市、增城市)水务工程建设工程质量监督检测业务,承担广州市(含从化市、增城市)水务工程平行、对比、第三方检测业务,为水务建设提供真实、可靠的数据,取得良好的成效。 我单位具有水利工程质量检测混凝土工程、岩土工程、量测和金属结构甲级资质。水文、水资源调查评价乙级资质、测绘丙级资质、水土保持方案编制乙级资质、安防资质、水平衡测试资质。</span>",\n  "fnIsotherprovinces" : "0",\n  "fcIntogdadress" : null,\n  "fdEffectivedate" : "2017-01-13 16:57:30",\n  "fcResponsibleofficer" : null,\n  "fcIndustrycode" : null,\n  "fdAudittime" : "2017-01-13 16:57:30",\n  "fcAuditperson" : "蔡惠芬",\n  "fnIsswotherprovinces" : "1",\n  "fcSwintogdadress" : "http://gcjs.gdwater.gov.cn/article.html?type=xy&menu_first=信用信息公开&menu_second=从业单位信用信息&menu_third=企业基本信息&id=3070&tableName=Bm211xydwQyjb&name=广州市水务科学研究所(质量检测)",\n  "fnIsjtotherprovinces" : "0",\n  "fcJtintogdadress" : null,\n  "fcBelongtrade" : null\n}'
content = json.loads(content)
print("content:", content)
# content: {'id': '2c9296bf597d9c4e0159865158763732', 'fcEntcode': 'GGZY02494', 'fcEntname': '广州市水务科学研究所', 'fcArtname': '李志威', 'fnArtpapertype': '1', 'fcArtpapersn': '420106196911205058', 'fcCountry': None, 'fcBelongareasn': '440100', 'fcBelongareasnName': '广州市', 'fcEntpropertysn': '110', 'fcIndustrysn': None, 'fcBusinesslicenseno': '124401004553512027', 'fcRegistrationno': None, 'fdBuilddate': '1978-04-29', 'fdBusinessauthority': '广州市事业单位登记管理局', 'fdVaildenddate': '2020-02-15', 'fcCreditgrade': None, 'fmEnrolfund': 1316, 'fcCurrencysn': None, 'fcEnrolbaseunit': None, 'fcEntinfodeclareperson': '李敏华', 'fdEntinfodeclaredate': None, 'fcEntinfodeclarepersontel': '13580421982', 'fcEntlinktel': '020-34289067', 'fcCaid': None, 'fcEntlinkaddr': '广州市海珠区前进路基立下道北12号', 'fcEntlinkzipcode': '                  ', 'fcEntlinkemail': 'sks2000@vip.com', 'fcRoletypesn': None, 'fnEntstatus': '1', 'fcFlowstatus': '3', 'fcAccount': '2c9296bf597d9c4e015986515875372e', 'fdCreatetime': '2017-01-10 10:59:55', 'fcCreator': 'gzsswkxyjs', 'fdModifytime': '2017-01-12 16:45:41', 'fcModifier': 'gzsswkxyjs', 'fcOrganizationcode': '124401004553512027', 'fcOrganizationauthority': '中华人民共和国国家质量监督检验检疫总局', 'fdSafelicencesdate': None, 'fdSafelicenceedate': None, 'fcSafelicencenumber': None, 'fcSafelicencecert': None, 'fcOrganizingauthority': None, 'fcOrganizingcode': None, 'fcBusinesstype': None, 'fcSubjectrole': None, 'fcRegistrationdept': None, 'fcRentregistrationno': None, 'fcRentregistrationdept': None, 'fcSocialno': None, 'fcSocialdept': None, 'fcOrganizationintro': '<span style="color:#000000;background-color:#F7FBFD;">广州市水务科学研究所(原广州市水利科学研究所,下称“水科所”)成立于1978年,是广州市水务局直属的事业单位,也是广州市水务系统唯一的科研机构。主要职能是:承担广州市水务科学研究、成果转化、科技咨询和科技推广与服务,推动水务行业科技进步。水科所建所以来一直致力于水务科研与新技术推得省、市科技进步奖二十多项,2009年度有3项产品获得广州市自主创新产品证书,有多项软广应用,为行业发展提供了有力的技术与支撑,先后获件获得中华人民共和国国家版权局颁发的计算机软件著作权登记证书上。1994年通过广东省技术监督局计量认证(CMA),2004年通过中国实验室认可委员会认可(CNAL),2009年通过中国国家合格评定委员会认可(CNAS), 是广东省水利行业首家通过CNAS实验室认可的检测单位,。 广州市水务科学研究所,即为广州市水务工程质量检测站,拥有一批国内领先的仪器设备和技术堪赞的资深专家及专业队伍,具备大型水利工程、市政供排水及其它建筑工程质量检测能力。始终以“公正、规范、准确、客观”为方针,以“一流技术、优质服务”为宗旨承接混凝土工程、岩土工程、地基与基础工程、金属结构、管材、量测、水量平衡等检测以及科研、咨询业务。承担广州市(含从化市、增城市)水务工程建设工程质量监督检测业务,承担广州市(含从化市、增城市)水务工程平行、对比、第三方检测业务,为水务建设提供真实、可靠的数据,取得良好的成效。 我单位具有水利工程质量检测混凝土工程、岩土工程、量测和金属结构甲级资质。水文、水资源调查评价乙级资质、测绘丙级资质、水土保持方案编制乙级资质、安防资质、水平衡测试资质。</span>', 'fnIsotherprovinces': '0', 'fcIntogdadress': None, 'fdEffectivedate': '2017-01-13 16:57:30', 'fcResponsibleofficer': None, 'fcIndustrycode': None, 'fdAudittime': '2017-01-13 16:57:30', 'fcAuditperson': '蔡惠芬', 'fnIsswotherprovinces': '1', 'fcSwintogdadress': 'http://gcjs.gdwater.gov.cn/article.html?type=xy&menu_first=信用信息公开&menu_second=从业单位信用信息&menu_third=企业基本信息&id=3070&tableName=Bm211xydwQyjb&name=广州市水务科学研究所(质量检测)', 'fnIsjtotherprovinces': '0', 'fcJtintogdadress': None, 'fcBelongtrade': None}
print(content.get("fcEntname"))
# 广州市水务科学研究所

每个公司都有自己的技术栈和风格,所以具体问题具体分析,此篇只做参考,也是自己太久没写爬虫遗忘了很多东西,所以做些记录,好记性不如烂笔头。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值