18537180361 发表于 2021-6-8 13:43:12

etree.html() 如何反向解析

      selector = etree.HTML(response.text)                                        #将字符串格式的html文档变成element对象
      book_list=selector.xpath("//*[@class='bang_list clearfix bang_list_mode']/li")#从element对象中提取一段
我现在想查看book_list里的内容。 但是现在是element对象我怎么给他变成字符串格式的。

wp231957 发表于 2021-6-8 14:23:52

https://fishc.com.cn/forum.php?mod=viewthread&tid=197052&mobile=2

18537180361 发表于 2021-6-8 14:46:59

wp231957 发表于 2021-6-8 14:23
https://fishc.com.cn/forum.php?mod=viewthread&tid=197052&mobile=2

打不开

wp231957 发表于 2021-6-8 15:23:55

18537180361 发表于 2021-6-8 14:46
打不开

那个是手机链接   把&mobile=2去掉应该可以了

你试试这个代码 吧

url="https://www.digikey.cn/en/products/filter/through-hole-resistors/53?s=N4IgrCBcoA5QjAGhDOl4AYMF9tA"
res=requests.get(url)
print(res.status_code)
tree = etree.HTML(res.text)
data = tree.xpath("//table[@id='data-table-0']/tbody/tr/td/div/div/div/a")
print(etree.tostring(data,encoding="utf-8" ,pretty_print=True).decode("utf-8"))
data = tree.xpath("//table[@id='data-table-0']/tbody/tr/td/div/div/div")
print(etree.tostring(data,encoding="utf-8" ,pretty_print=True).decode("utf-8"))
页: [1]
查看完整版本: etree.html() 如何反向解析