背景】
之前写的,去处理本地已有的一个html文件,
然后对于提取出来的信息,导出为,各种形式的json字符串。
【scrape_html_to_json代码分享】
1.截图:
(1)运行效果:
(2)输出的各种json字符串:
A。无格式化,无缩进:
1 | [{ "yearMonth" : { "month" : { "string" : "November" , "value" : "11" }, "year" : { "string" : "2012" , "value" : "2012" }}, "reservedMonthList" : [ "2" , "3" , "8" , "9" , "10" , "11" , "12" , "13" , "17" , "18" , "19" , "20" , "21" , "22" , "23" ]}, { "yearMonth" : { "month" : { "string" : "December" , "value" : "12" }, "year" : { "string" : "2012" , "value" : "2012" }}, "reservedMonthList" : [ "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" , "15" , "16" , "21" , "22" , "23" , "24" , "25" , "26" , "27" , "28" , "30" , "31" ]}] |
B。普通的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | $calendar = { "listing_id1" :{ "1" : { "start_date" : 2/11/2012, "end_date" : 3/11/2012, "status" : reserved }, "2" : { "start_date" : 8/11/2012, "end_date" : 13/11/2012, "status" : reserved }, "3" : { "start_date" : 17/11/2012, "end_date" : 23/11/2012, "status" : reserved },}, "listing_id2" :{ "1" : { "start_date" : 7/12/2012, "end_date" : 16/12/2012, "status" : reserved }, "2" : { "start_date" : 21/12/2012, "end_date" : 28/12/2012, "status" : reserved }, "3" : { "start_date" : 30/12/2012, "end_date" : 31/12/2012, "status" : reserved },}, "listing_id3" :{ "1" : { "start_date" : 1/1/2013, "end_date" : 10/1/2013, "status" : reserved },}, "listing_id4" :{ "1" : { "start_date" : 1/2/2013, "end_date" : 27/2/2013, "status" : reserved },}, "listing_id5" :{}, "listing_id6" :{ "1" : { "start_date" : 2/4/2013, "end_date" : 30/4/2013, "status" : reserved },}, "listing_id7" :{ "1" : { "start_date" : 1/5/2013, "end_date" : 31/5/2013, "status" : reserved },}, "listing_id8" :{ "1" : { "start_date" : 1/6/2013, "end_date" : 30/6/2013, "status" : reserved },}, "listing_id9" :{ "1" : { "start_date" : 1/7/2013, "end_date" : 31/7/2013, "status" : reserved },}, "listing_id10" :{ "1" : { "start_date" : 1/8/2013, "end_date" : 31/8/2013, "status" : reserved },}, "listing_id11" :{ "1" : { "start_date" : 1/9/2013, "end_date" : 30/9/2013, "status" : reserved },}, "listing_id12" :{ "1" : { "start_date" : 1/10/2013, "end_date" : 31/10/2013, "status" : reserved },}, } |
C。带缩进的格式化的json:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | [ { "yearMonth" : { "month" : { "string" : "November" , "value" : "11" }, "year" : { "string" : "2012" , "value" : "2012" } }, "reservedMonthList" : [ "2" , "3" , "8" , "9" , "10" , "11" , "12" , "13" , "17" , "18" , "19" , "20" , "21" , "22" , "23" ] }, { "yearMonth" : { "month" : { "string" : "December" , "value" : "12" }, "year" : { "string" : "2012" , "value" : "2012" } }, "reservedMonthList" : [ "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" , "15" , "16" , "21" , "22" , "23" , "24" , "25" , "26" , "27" , "28" , "30" , "31" ] } ] |
注:以上内容不全部相同。只是为了显示效果。
2.Python项目代码下载:
scrape_html_to_json_2012-11-08.7z
3.代码分享:
(1)scrape_html_to_json.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | #!/usr/bin/python # -*- coding: utf-8 -*- """ ------------------------------------------------------------------------------- Function: Web Scraper Version: 2012-11-08 Author: Crifan Li Contact: https://www.crifan.com/about/me/ ------------------------------------------------------------------------------- """ #---------------------------------import--------------------------------------- import re; import sys; sys.path.append( "libs" ); #import urllib; import codecs; from string import Template,replace; import json; from BeautifulSoup import BeautifulSoup,Tag,CData; #------------------------------------------------------------------------------ # from ['2','3','8','9','10','11','12','13','17','18','19','20','21','22','23'] # to #[ #{ #'startDay': 2, #'endDay' : 3, #}, #{ #'startDay': 8, #'endDay' : 13, #}, #{ #'startDay': 17, #'endDay' : 23, #}, #] def generateDurationDictList(monthList): durationMonthDictList = []; #print "monthList=",monthList; if (monthList): monthIntList = []; for eachMonthStr in monthList: monthIntList.append( int (eachMonthStr)); monthIntList.sort(); #print "monthIntList=",monthIntList; curStartMonth = monthIntList.pop( 0 ); #print "curStartMonth=",curStartMonth; curEndMonth = curStartMonth; curInterMonth = curStartMonth; startFindNewDuration = False ; while (monthIntList): currentMonthInt = monthIntList.pop( 0 ); #print "---currentMonthInt=",currentMonthInt; if (currentMonthInt = = (curInterMonth + 1 )): startFindNewDuration = True ; curInterMonth = curInterMonth + 1 ; #print "after add 1, curInterMonth=",curInterMonth; else : durationInfoDict = { 'startDay' : curStartMonth, 'endDay' : curInterMonth, }; durationMonthDictList.append(durationInfoDict); startFindNewDuration = False ; curEndMonth = currentMonthInt; curStartMonth = currentMonthInt; curInterMonth = currentMonthInt; if (startFindNewDuration): startFindNewDuration = False ; durationInfoDict = { 'startDay' : curStartMonth, 'endDay' : currentMonthInt, }; durationMonthDictList.append(durationInfoDict); #print "durationMonthDictList=",durationMonthDictList; #else: #print "input monthList is null"; return durationMonthDictList; #------------------------------------------------------------------------------ def generateOutputCalendar(MonthDictList): #print "MonthDictList=",MonthDictList; wholeStr = ""; headerStr = "$calendar = {" ; tailStr = "}" ; allMonthStr = ""; for index,eachMonthDict in enumerate (MonthDictList): number = index + 1 ; singleMonthWholeStr = ""; monthHeaderStr = '"listing_id' + str (number) + '":{' ; monthTailStr = "}," ; monthDurationListStr = ""; #print "============ now process year=%s, month=%s"%(eachMonthDict['yearMonth']['year']['string'], eachMonthDict['yearMonth']['month']['string']); durationInfoDictList = generateDurationDictList(eachMonthDict[ 'reservedMonthList' ]); for durationIdx,eachDurationDict in enumerate (durationInfoDictList): durationNum = durationIdx + 1 ; singelDurationT = Template( """"${number}": {"start_date": ${startDay}/${startMonth}/${startYear}, "end_date": ${endDay}/${endMonth}/${endYear}, "status": reserved },""" ); singleDurationDict = { 'number' : durationNum, 'startDay' : eachDurationDict[ 'startDay' ], 'startMonth' : eachMonthDict[ 'yearMonth' ][ 'month' ][ 'value' ], 'startYear' : eachMonthDict[ 'yearMonth' ][ 'year' ][ 'value' ], 'endDay' : eachDurationDict[ 'endDay' ], 'endMonth' : eachMonthDict[ 'yearMonth' ][ 'month' ][ 'value' ], 'endYear' : eachMonthDict[ 'yearMonth' ][ 'year' ][ 'value' ], }; # "1": # {"start_date": 11/7/2012, # "end_date": 11/9/2012, # "status": reserved # }, singelDurationStr = singelDurationT.substitute(singleDurationDict); #print "singelDurationStr=",singelDurationStr; monthDurationListStr + = singelDurationStr; singleMonthWholeStr = monthHeaderStr + monthDurationListStr + monthTailStr; #print "singleMonthWholeStr=",singleMonthWholeStr; allMonthStr + = singleMonthWholeStr + "\r\n\r\n" ; wholeStr = headerStr + allMonthStr + tailStr; #print "wholeStr=",wholeStr; return wholeStr; #------------------------------------------------------------------------------ def generateOutputCalendarJsonNoIndent(MonthDictList): jsonDumpsNoIndent = json.dumps(MonthDictList); #print "jsonDumpsNoIndent=",jsonDumpsNoIndent; return jsonDumpsNoIndent; #------------------------------------------------------------------------------ def main(): foundSingleAttrFromUrl = re.search( "http:.+?\?(?P<singleAttr>\w+)=.*?" , testEntryUrl); #print "foundSingleAttrFromUrl=",foundSingleAttrFromUrl; if (foundSingleAttrFromUrl): singleAttr = foundSingleAttrFromUrl.group( "singleAttr" ); print "Extract singleAttr=%s from testEntryUrl=%s" % (singleAttr, testEntryUrl); testFilename = "testfiles/test_scrape.htm" ; htmlFile = codecs. open (testFilename, 'r' , "UTF-8" ); #print "htmlFile=",htmlFile; testHtml = htmlFile.read(); #print "testHtml=",testHtml; soup = BeautifulSoup(testHtml); #<table border="0" cellpadding="2" cellspacing="0" class="text" width="100%"> foundAllMonthHeader = soup.findAll(name = "table" , attrs = { "class" : "text" }); #print "foundAllMonthHeader=",foundAllMonthHeader; monthHeaderLen = len (foundAllMonthHeader); #print "monthHeaderLen=",monthHeaderLen; #<table border="1" class="CalendarCellActive" cellpadding="2" cellspacing="0" style=" border: 1px solid navy; table-layout:fixed" width="100%"> foundAllMonthContent = soup.findAll(name = "table" , attrs = { "class" : "CalendarCellActive" }); #print "foundAllMonthContent=",foundAllMonthContent; monthContentLen = len (foundAllMonthContent); #print "monthContentLen=",monthContentLen; print "Total found %d month's info of reserved days" % (monthContentLen); MonthDictList = []; for i,eachMonthHeader in enumerate (foundAllMonthHeader): singleMonthDict = { 'yearMonth' :{ 'year' : { 'value' : "", 'string' : "", }, 'month' : { 'value' : "", 'string' : "", }, }, 'reservedMonthList' :[], # each one is singel string of month }; #Note: #here, actually, the simplest method to extract the year and month label is: #just find two label, then consider the first is month and second is year # foundTwoLabel = eachMonthHeader.findAll("label"); # print "foundTwoLabel=",foundTwoLabel; # monthLabel = foundTwoLabel[0]; # yearLabel = foundTwoLabel[1]; # monthStr = monthLabel.string; # yearStr = yearLabel.string; # print "monthStr=",monthStr; # monthStr= November # print "yearStr=",yearStr; # yearStr= 2012 # but that kind of method is not safe and robust #so use following code # <td style="padding-left:0" width="60%"><label>November</label> # <input type="Hidden" id="cboMonth1" name="cboMonth1" value="11"> # </td><td style="padding-right:0;" width="40%"> # <label>2012</label> # <input type="Hidden" id="cboYear1" name="cboYear1" value="2012"> # </td> foundCboMonth = eachMonthHeader.find( "input" , { "id" :re. compile ( "cboMonth\d+" )}); #print "foundCboMonth=",foundCboMonth; monthValue = foundCboMonth[ 'value' ]; #print "monthValue=",monthValue; tdMonth = foundCboMonth.parent; #print "tdMonth=",tdMonth; tdMonthLabel = tdMonth.label; #print "tdMonthLabel=",tdMonthLabel; monthStr = tdMonthLabel.string; #print "monthStr=",monthStr; foundCboYear = eachMonthHeader.find( "input" , { "id" :re. compile ( "cboYear\d+" )}); #print "foundCboYear=",foundCboYear; yearValue = foundCboYear[ 'value' ]; #print "yearValue=",yearValue; tdYear = foundCboYear.parent; #print "tdYear=",tdYear; tdYearLabel = tdYear.label; #print "tdYearLabel=",tdYearLabel; yearStr = tdYearLabel.string; #print "yearStr=",yearStr; singleMonthDict[ 'yearMonth' ][ 'month' ][ 'string' ] = monthStr; singleMonthDict[ 'yearMonth' ][ 'month' ][ 'value' ] = monthValue; singleMonthDict[ 'yearMonth' ][ 'year' ][ 'string' ] = yearStr; singleMonthDict[ 'yearMonth' ][ 'year' ][ 'value' ] = yearValue; # extract the necessary content: the reserved days eachMonthContent = foundAllMonthContent[i]; #<td align="center" class="CalendarCellReserved" id="dd1">2</td> foundAllReservedCell = eachMonthContent.findAll( "td" , { "class" : "CalendarCellReserved" }); #print "foundAllReservedCell=",foundAllReservedCell; reservedCellNum = len (foundAllReservedCell); #print "reservedCellNum=",reservedCellNum; for eachReservedCell in foundAllReservedCell: cellVal = eachReservedCell.string; #print "cellVal=",cellVal; singleMonthDict[ 'reservedMonthList' ].append(cellVal); #print "singleMonthDict=",singleMonthDict; MonthDictList.append(singleMonthDict); #print str(i+1) + "="*79; print "Processed %d month's info" % (i + 1 ); # generate output string generatedCalendarStr = generateOutputCalendar(MonthDictList); #print "generatedCalendarStr=",generatedCalendarStr; outputFileName = "generatedCalerdarString.txt" ; print "Exporting generated calendar string into %s" % (outputFileName); outputFile = codecs. open (outputFileName, 'w' , 'utf-8' ); outputFile.write(generatedCalendarStr); outputFile.close(); print "Has exported calendar string into %s" % (outputFileName); # Note: # only makesure your expected output is somthing like: # {"start_date": "11/7/2012", # "end_date": "11/9/2012", # "status": "reserved" # }, # not : # {"start_date": 11/7/2012, # "end_date": 11/9/2012, # "status": reserved # }, # then I can use json to ouptut PRETTY-PRINTED dict string #------------------------------------------------------------------------------ def generateOutputCalendarJsonIndent(MonthDictList): jsonDumpsIndent = json.dumps(MonthDictList, indent = 1 ); #print "jsonDumpsIndent=",jsonDumpsIndent; return jsonDumpsIndent; # json ouput demo demoDictList = MonthDictList[ 0 : 2 ]; jsonDumpsIndentStr = json.dumps(demoDictList, indent = 1 ); outputFile_json_indent = "treeLikeWithIndentJsonString.txt" ; outputFile_json_indent = codecs. open (outputFile_json_indent, 'w' , 'utf-8' ); outputFile_json_indent.write(jsonDumpsIndentStr); outputFile_json_indent.close(); #tttttttt generatedCalendarJsonIndentStr = generateOutputCalendarJsonIndent(demoDictList); print "type(generatedCalendarJsonIndentStr)=" , type (generatedCalendarJsonIndentStr); #print "generatedCalendarJsonIndentStr=",generatedCalendarJsonIndentStr; outputFileName_json_indent = "generatedCalerdarString_json_indent.txt" ; print "Exporting generated calendar string json indent into %s" % (outputFileName_json_indent); outputFile_json_indent = codecs. open (outputFileName_json_indent, 'w' , 'utf-8' ); outputFile_json_indent.write(generatedCalendarJsonIndentStr); outputFile_json_indent.close(); generatedCalendarJsonNoIndentStr = generateOutputCalendarJsonNoIndent(demoDictList); print "type(generatedCalendarJsonNoIndentStr)=" , type (generatedCalendarJsonNoIndentStr); #print "generatedCalendarJsonNoIndentStr=",generatedCalendarJsonNoIndentStr; outputFileName_json_noIndent = "generatedCalerdarString_json_noIndent.txt" ; print "Exporting generated calendar string json no indent into %s" % (outputFileName_json_noIndent); outputFile_json_noIndent = codecs. open (outputFileName_json_noIndent, 'w' , 'utf-8' ); outputFile_json_noIndent.write(generatedCalendarJsonNoIndentStr); outputFile_json_noIndent.close(); ############################################################################### if __name__ = = "__main__" : main(); |
【总结】
转载请注明:在路上 » 【代码分享】Python代码:scrape_html_to_json – 从本地html中抓取信息导出为各种形式的json字符串