Skip to content
Advertisement

How to extract string value in html with parsing in python

I am trying to get the string value for each link. (For example, like Pennsylvania)

JavaScript

But since there are title and id attributes, I am a bit confused about how to do it. I get a null result when I display my array. Here is my code :

JavaScript

Advertisement

Answer

Use .stripped_strings to generate a list of strings of elements in your selection and pick / slice the result – In this case pick first element to get Pennsylvania:

JavaScript

Note In new code find_all() should be used, findAll() actually still works but is very old syntax

To get the href:

JavaScript

Example

With multiple li tags:

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement