scrapy教程

<Element div at 0x2b85af8>形式转换成字符串

有时候,在我们用xpath获取的一些形式为<Element div at 0x2b8hi5af8>时候,就需要转换成我们能看懂的字符串形式,可以用两个方法:

import requests
from lxml import etree
url = 'http://www.youtx.com/room/67827/'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36", }
body = requests.get(url,headers=headers).text
html = etree.HTML(body)
div_ele = html.xpath('//div[contains(@class,"lists") and contains(@class,"clearfix")]')[3]
print(div_ele) #<Element div at 0x2b37f30>
# print(hhh.xpath('./p/text()'))
cn = etree.tounicode(div_ele)
cn2 = etree.tostring(div_ele)
print(cn)
print(cn2)


最后修改:2021-08-26 15:48:01