TypeError:需要类似字节的对象,而不是' Tag'

时间:2016-11-23 02:21:05

标签: python html xml beautifulsoup elementtree

将s输出传递给>>时出现标记错误table = ET.XML(s)

response = response = requests.get('https://sandbox.jiveon.com/api/core/v3/contents/436669', auth=('dedileep', 'Xento123$'))
data = response.json()
html_doc = data['content']['text']
soup = BeautifulSoup(html_doc, 'html5lib')
s = soup.find('table', attrs={'class':'mytable'})
def find_version(ver):
    table = ET.XML(s) 

这里引发错误-----

# headers    Traceback (most recent call last):
  File "final_release.py", line 63, in <module>
    res = find_version(ver)
  File "final_release.py", line 25, in find_version
    table = ET.XML(s)
  File "C:\Users\dedileep\AppData\Local\Programs\Python\Python35-32\lib\xml\etree\ElementTree.py", line 1333, in XML
parser.feed(text)
TypeError: a bytes-like object is required, not 'Tag'


ths = [th.text for th in table.findall('.//th')]
    for tr in table.findall('.//tbody/tr'):
        data = []
        data.append(tr[0].text)
        data.append([x.text for x in tr[1]])
        temp = []
        for x in tr[2]:
            if x.tag == 'a':
                temp.append(x.text)
            else:
                temp.append(x[0].text)
        data.append(temp)
        out = OrderedDict(zip(ths, data))
        print('out:', out)
        if out['Release'] == ver:
            return out

节目主要
    res = find_version(ver)      如果res:             for res,res initems()中的val:                 打印(键,&#39; - &#39;,val)         其他:             打印(&#39;未找到版本&#39;)

1 个答案:

答案 0 :(得分:0)

s = soup.find('table', attrs={'class':'mytable'}) #return a Tag object

ET.XML(s) #require a bytes-like object, like string

溶液

s = soup.find('table', attrs={'class':'mytable'}).get_text()

它返回文档中或标记下的所有文本,作为单个Unicode字符串,但我不确定它是您想要的。