测试代码:import requests
from lxml import etree
a = '''
<a id="main-content"></a>
<div class="report-check-survey"><p>How can we improve Report Check? <a target="_blank">Take this quick survey</a>.</p></div>
<!-- No Match -->
<section id='no-match'>
<div class='content'>
<form action='/sites/Satellite' class='search-box report-lookup-form' method='GET'>
<span style="display:none;" name="xmlcontent" id="xmlcontent">
<REPORT_CHECK_RESPONSE><STATUS>SUCCESS</STATUS><ERROR_DTLS><ERROR_CODE></ERROR_CODE><ERROR_MSG></ERROR_MSG></ERROR_DTLS><REPORT_DTLS><REPORT_DTL><MESSAGE></MESSAGE><LENGTH>4.46 x 4.38 x 3.19 mm</LENGTH><WIDTH>4.46 x 4.38 x 3.19 mm</WIDTH><DEPTH>4.46 x 4.38 x 3.19 mm</DEPTH><WEIGHT>0.55</WEIGHT><REPORT_NO>6342219172</REPORT_NO><COLOR>E</COLOR><COLOR_DESCRIPTIONS></COLOR_DESCRIPTIONS><CLARITY>SI1</CLARITY><FINAL_CUT></FINAL_CUT><DEPTH_PCT>72.8</DEPTH_PCT><TABLE_PCT>72</TABLE_PCT><CRN_AG></CRN_AG><CRN_HT></CRN_HT><PAV_AG></PAV_AG><PAV_DP></PAV_DP><STR_LN></STR_LN><LR_HALF></LR_HALF><GIRDLE>Thin to Very Thick</GIRDLE><GIRDLE_CONDITION></GIRDLE_CONDITION><GIRDLE_PCT></GIRDLE_PCT><CULET_SIZE>None</CULET_SIZE><POLISH>Excellent</POLISH><SYMMETRY>Very Good</SYMMETRY><FLUORESCENCE_INTENSITY>None</FLUORESCENCE_INTENSITY><FLUORESCENCE_COLOR></FLUORESCENCE_COLOR><KEY_TO_SYMBOLS>Crystal</KEY_TO_SYMBOLS><REPORT_TYPE>DD~Diamond Dossier</REPORT_TYPE><REPORT_DT>12/03/2019</REPORT_DT><INSCRIPTION>GIA 6342219172</INSCRIPTION><SHAPE>SMB~Square Modified Brilliant</SHAPE><REPORT_COMMENTS></REPORT_COMMENTS><CONTROL_NUMBER>0312F1B207335FEE548803C4910B278B</CONTROL_NUMBER><COUNTRY_OF_ORIGIN></COUNTRY_OF_ORIGIN><INCLUSION_DTLS/><CLARITY_STATUS_CODE></CLARITY_STATUS_CODE><CLARITY_STATUS_ABBR></CLARITY_STATUS_ABBR><CUT_CODE></CUT_CODE><POLISH_CODE>EX</POLISH_CODE><SYMMETRY_CODE>VG</SYMMETRY_CODE><FLUO_INTENSITY_CODE>NON</FLUO_INTENSITY_CODE><GIRDLE_CODE>THN to VTK</GIRDLE_CODE><CULET_CODE>NON</CULET_CODE><LENGTH_CODE>4.46</LENGTH_CODE><WIDTH_CODE>4.38</WIDTH_CODE><DEPTH_CODE>3.19</DEPTH_CODE><CSS_COLOR_CODE>E</CSS_COLOR_CODE><CSS_COLOR_DESC></CSS_COLOR_DESC><PAINTING></PAINTING><PAINTING_COMMENT></PAINTING_COMMENT><PROPORTION></PROPORTION><SYNTHETIC_INDICATOR>NO</SYNTHETIC_INDICATOR><IDENT_TBL_REPORT_DT></IDENT_TBL_REPORT_DT><IDENT_TBL_WEIGHT></IDENT_TBL_WEIGHT><IDENT_TBL_MEASUREMENTS></IDENT_TBL_MEASUREMENTS><IDENT_TBL_SHAPE></IDENT_TBL_SHAPE><IDENT_TBL_CUTTINGSTYLE></IDENT_TBL_CUTTINGSTYLE><IDENT_TBL_CUTTINGSTYLE_PAV></IDENT_TBL_CUTTINGSTYLE_PAV><IDENT_TBL_CUTTINGSTYLE_CRN></IDENT_TBL_CUTTINGSTYLE_CRN><IDENT_TBL_TRANSPARENCY></IDENT_TBL_TRANSPARENCY><IDENT_TBL_COLOR></IDENT_TBL_COLOR><IDENT_TBL_PHENOMENON></IDENT_TBL_PHENOMENON><IDENT_TBL_DESCRIPTION></IDENT_TBL_DESCRIPTION><IDENT_TBL_GROUP></IDENT_TBL_GROUP><IDENT_TBL_TRADENAME></IDENT_TBL_TRADENAME><IDENT_TBL_SPECIES></IDENT_TBL_SPECIES><IDENT_TBL_VARIETY></IDENT_TBL_VARIETY><IDENT_TBL_SOURCETYPE></IDENT_TBL_SOURCETYPE><IDENT_TBL_GEOGRAPHICORIGIN></IDENT_TBL_GEOGRAPHICORIGIN><IDENT_TBL_TREATEMENT></IDENT_TBL_TREATEMENT><IDENT_TBL_COMMENTS></IDENT_TBL_COMMENTS><IDENT_TABULAR_INDICATOR></IDENT_TABULAR_INDICATOR><IDENT_NAR_DESC></IDENT_NAR_DESC><IDENT_NAR_CONCLUSION></IDENT_NAR_CONCLUSION><IDENT_NAR_COMMENTS></IDENT_NAR_COMMENTS><QUANTITY></QUANTITY><MEASUREMENTS></MEASUREMENTS><PEARLS></PEARLS><ENVIRONMENT></ENVIRONMENT><MOLLUSK></MOLLUSK><TREATMENTS></TREATMENTS><BODYCOLOR></BODYCOLOR><OVERTONE></OVERTONE><LUSTER></LUSTER><SURFACE></SURFACE><NACRETHICKNESS></NACRETHICKNESS><MATCHING></MATCHING><DRILLING></DRILLING><REPORT_DESCRIPTION></REPORT_DESCRIPTION><GENERAL_DESC></GENERAL_DESC><IS_PDF_AVAILABLE>TRUE</IS_PDF_AVAILABLE><EREPORT_URL></EREPORT_URL><TREATMENT_URLS></TREATMENT_URLS><MATERIAL></MATERIAL><SEALING_CODE></SEALING_CODE><REPORT_SLEEVE_MSG></REPORT_SLEEVE_MSG><INFO_MSG></INFO_MSG><KTS_IMG></KTS_IMG><DIAMETER_RANGE></DIAMETER_RANGE><MELEE_COUNT></MELEE_COUNT><TEST_RESULT_TYPE></TEST_RESULT_TYPE><MELEE_MSG></MELEE_MSG><DIGITAL_RPT_FLG>N</DIGITAL_RPT_FLG></REPORT_DTL></REPORT_DTLS></REPORT_CHECK_RESPONSE>
</span>
<input class='icon-search form-control' name='reportno' placeholder='Enter Report No.' type='tel' />
<input type='submit' name='' id='' class='button-submit ' value='Go' />
<input type="hidden" name="c" value="Page"/>
<input type="hidden" name="childpagename" value="GIA/Page/ReportCheck"/>
<input type="hidden" name="pagename" value="GIA/Wrapper"/>
<input type="hidden" name="cid" value="1495275503754"/>
<input type="hidden" name="encryptedString" id="encryptedString" value="ADB8EFD5E8B146D156516E7BE68FBD8D"/>
<input type="hidden" name="qr" id="qr" value="null"/>
</form>
'''
tree = etree.HTML(a)
content = tree.xpath('//span[@style="display:none;"]')[0]
result = etree.tostring(content, pretty_print=True, method='html').decode('utf-8')
print(result)
应该可以成功运行的代码:import requests
from lxml import etree
url = 'https://www.gia.edu/sites/Satellite?reportno=6342219172&c=Page&childpagename=GIA%2FPage%2FReportCheck&pagename=GIA%2FWrapper&cid=1495275503754'
headers = {
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36'
}
res = requests.get(url, headers=headers)
tree = etree.HTML(res.text)
content = tree.xpath('//span[@style="display:none;"]')[0]
result = etree.tostring(content, pretty_print=True, method='html').decode('utf-8') # 转为HTML源码
print(result)
|