简体   繁体   English

beautifulsoup 对象如何能够将标签作为属性?

[英]How beautifulsoup objects able to have a tag as an attribute?

Inorder to extract a tag, you need to use the tag as an attribute to the Tag / BeautifulSoup object, eg To extract the <head> tag, I need to do this soupobject.head为了提取一个标签,你需要使用这个标签作为一个属性到Tag / BeautifulSoup object,例如要提取<head>标签,我需要做这个soupobject.head

I'm still beginner in programming and python but from my understanding and quick google search, object attributes are variables belonging to that objects.我仍然是编程和 python 的初学者,但根据我的理解和快速的谷歌搜索,object 属性是属于该对象的变量。 I mean I can write a script that have a variable named p and have a condition that when my script run, if it find a <p> tag, it will then parse any relevant data from it and then assign it to the p variable I made, but to write a script that itself will "define" a variable and name it according to html tag name that I don't know how.我的意思是我可以编写一个脚本,它有一个名为p的变量,并且有一个条件,当我的脚本运行时,如果它找到一个<p>标记,它将从中解析任何相关数据,然后将其分配给p变量 I做了,但要编写一个脚本,它本身将“定义”一个变量并根据我不知道如何的 html 标记名称命名它。

I hope I explaining it enough.我希望我解释得足够多。 I tried to understand the beautifulsoup source code but honestly I still having trouble understanding most of it.我试图理解 beautifulsoup 源代码,但老实说,我仍然无法理解其中的大部分内容。

My only assumption/theory on how it able to that, is by creating a string format of a python code then import that, I don't know if that possible我唯一的假设/理论是通过创建 python 代码的字符串格式然后导入它,我不知道这是否可能

In general, it is not considered a good practice to have varaible variable names.一般来说,使用可变变量名并不是一个好习惯。 Some languages even make it impossible to do so.有些语言甚至无法做到这一点。 In order to achieve the same thing, you can use a dictionary object which can have variable key-names and variable values.为了达到同样的目的,您可以使用字典 object,它可以具有可变键名和可变值。

my_dict = {'key_1': 'value 1'}
print(my_dict['key_1'])
# out: 'value 1'

my_dict['some_key'] = 'another value'
# now your dictionary looks like this: 
# {'key_1': 'value 1', 'some_key': 'another value'}
print(my_dict['some_key'])
# out: 'another value'

# as for dynamic names:
some_name = 'key_3'
my_dict[some_name] = 'value 3'
print(my_dict)
# out: {'key_1': 'value 1', 'some_key': 'another value', 'key_3': 'value 3'}

Have a look at data model class customization via special methods and particularly at customizing attribute access via __getattr__() and __getattribute__() magic methods 通过特殊方法查看数据 model class 自定义,特别是通过__getattr__()__getattribute__()魔术方法自定义属性访问

In this particular case ( bs4 ), you can have a look at bs4 source code for Tag class, where they define Tag.__getattr__() magic method .在这种特殊情况下( bs4 ),您可以查看Tag class 的 bs4 源代码,其中定义了Tag.__getattr__()魔术方法 Note that BeautifulSoup class inherits from Tag注意BeautifulSoup class 继承自Tag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM