Web Scraping遇到XML解析错误,如何解决?

问题描述 投票:0回答:1

我已经使用Web抓取应用程序大约一年了,没有任何实际问题。今天早上,我运行了程序,并从xml.etree中得到了不匹配的标签错误。这是在今天早上之前从未发生过的,所以对于现在突然发生的原因我有些困惑。这是我的代码:

import requests
from xml.etree import ElementTree as ET
import json
import datetime as dt
import time
from dateutil import parser
from bs4 import BeautifulSoup
from xml.parsers import expat


url = 'https://www5.fdic.gov/cra/WebServices/DBService.asmx/callWS'

r = requests.post(url, data={"functionName":"SearchCRA","parmsJSON":"{\"Appl_Number\":\"\",\"Appl_Type\":\"\",\"PSTALP\":\"\",\"SUPRV_FDICDBS\":\"09\",\"BANK_NAME\":\"\"}"})
soup = BeautifulSoup(r.text, 'html.parser')

root = ET.fromstring(r.content)
data = json.loads(root.text)

today = dt.date.today()
lastweek = today-dt.timedelta(7)
date = lastweek.strftime("%m/%d/%Y") #one week from today in mm/d/yyyy

mylist = []
for result in data['Result']:
    d = parser.parse(result['Appl_Recd_YMD'])
    f_d = d.strftime("%m/%d/%Y")

    if f_d >= date:
        new_status = "***NEW***"
    else:
        new_status = "        "

if 'INTERIM' not in result['Instname'] and result['Inst_Rle_Cde'] == '1' and result['Appl_Type'] in ('REORG ', 'MERGER', 'RELMO', 'FDINEW'):
    output4 = 'Date: {} Application Number: {} Institution: {} State: {} Type: {} Link: https://www5.fdic.gov/cra/cram03.aspx?inApplNb={}&inApplType={} {}'.format(f_d, result['Appl_Number'], result['Instname'].strip(),result['Pstalp'], result['Appl_Type'],result['Appl_Number'], result['Appl_Type'], new_status)
    item = output4
    mylist.append(item)
    slist = sorted(mylist)
    print(len(mylist), end=""),
    print('.)', end = ""),
    print(output4)
    global slist2
    slist2 = slist

这是我得到的错误:

  File "C:/Users/d1rjr03/PycharmProjects/Discovery/FDIC.py", line 16, in <module>
root = ET.fromstring(r.content)
  File "C:\Python37\lib\xml\etree\ElementTree.py", line 1315, in XML
parser.feed(text)
xml.etree.ElementTree.ParseError: mismatched tag: line 7, column 2

我对xml.etree没有太多的经验,所以对于解决这种情况,我不确定从哪里开始。知道为什么会突然发生吗?

python xml xml-parsing python-requests xml.etree
1个回答
0
投票

在将r.content传递给ET.fromstring之前先看一下它的值,这是您所期望的吗?将其打印到屏幕上,将其写入日志文件或使用调试器运行代码并检查该值。从错误中可以看出,该值将不是您认为的值]

© www.soinside.com 2019 - 2024. All rights reserved.