[英]how to get the objectname from <class 'lxml.html.HtmlElement'>
from pyquery import PyQuery as pq
print(pq(url='https://www.bing.com')('div'))
if I run code in terminal, only output this如果我在终端运行代码,只有 output 这个
<div id="hp_container"><div id="bgDiv"><div id="bgImgProgLoad" data-ultra-definition-src="/th?id=OHR.AltaFloresta_...
from pyquery import PyQuery as pq
pq(url='https://www.bing.com')('div')
When I run the above code in jupyterab, I get following result当我在 jupyterab 中运行上述代码时,我得到以下结果
[<div#hp_container>, <div#bgDiv>, <div#bgImgProgLoad>, ...]
The selector of each div tag is automatically displayed in the result, such like div#hp_container , Now I can only see them in the output in jupyterlab, I don't know what kind of attribute or method to get each selector name itself.每个 div 标签的选择器会自动显示在结果中,比如div#hp_container ,现在只能在 jupyterlab 的 output 中看到,不知道是通过什么属性或者方法自己获取每个选择器名称的。
================================= ==================================
2021-05-15 Comment Add Picture 2021-05-15 评论 添加图片
I mean when I enter x
, I can see a list of selectors, I just don't know how to convert it to a list of str我的意思是当我输入
x
时,我可以看到选择器列表,我只是不知道如何将其转换为 str 列表
The 'selector', or rather the classes of each element are available as a .classes
iterable on the element: “选择器”,或者更确切地说,每个元素的类可作为元素上的
.classes
可迭代使用:
from pyquery import PyQuery as pq
for div in pq(url='https://www.bing.com')('div'):
print(list(div.classes))
Result:结果:
['hpapp']
['hp_body']
['hpl']
['img_cont']
Etc. (casting to string for printing)等(转换为字符串进行打印)
If you simply want to display the element names and classes as JupyterLab apparently does for you:如果您只是想像 JupyterLab 显然为您那样显示元素名称和类:
for elem in pq(url='https://www.bing.com')('div'):
print(f'{elem.tag}#{".".join(elem.classes)}')
Result:结果:
div#hpapp
div#hp_body
div#hpl
div#img_cont
Etc.等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.