I'm currently writing a python HTML parser. When storing attributes and values of tags, I have come across HTML attributes with no assigned value. This is some HTML that I scraped off Github, which is when I encountered the error through testing.
<meta name="request-id" content="someRandomValue" data-pjax-transient>
<span class="js-user-status-original-emoji" hidden></span>
Since I'm storing attributes as keys in a dictionary, I need a value for the key-value pair. What value does an HTML attribute with no assigned value have? What would the values of "hidden" and "data-pjax-transient" be?
-
The value is irrelevant, so you can substitute absolutely anything. See developer.mozilla.org/en-US/docs/Web/HTML/…Robin Zigmond– Robin Zigmond2019年12月26日 09:50:16 +00:00Commented Dec 26, 2019 at 9:50
4 Answers 4
It doesn't have a value, it's just a marker or flag. Logically, you can think of it as a boolean value that's true, if the flag is present, or false otherwise.
3 Comments
hidden="True" or hidden="lorem ipsum dolor" means the same thing. As long as the attribute hidden exists (with or without a value), it's interpreted as being true. If hidden does not exist, then it's interpreted as being false.True as the value.Usually if there is now value associated with attribute it is considered true.
So in your case
<span class="js-user-status-original-emoji" hidden></span>,
for attribute hidden, you can store true as value.
Comments
HTML Attributes with no assigned values are implicitly given the value of the empty string. However, they are usually used to enable a certain boolean attribute (such as disabling a button). For example,
if (element.dataset.disabled !== undefined) {
// Prevent user from clicking button
}
Comments
Simply use None in the dictionary as a value and put an if condition in the Python script.