BeautifulSoup4 retrieves the value of the class attribute of the tag
Recently, when I was writing a crawler, I suddenly needed to determine whether the class value of the current tag was a specific value. After searching online, I found that it was not very helpful, so I made a note of it.
Using the GET method
html = BeautifulSoup(request,'lxml')
a = html.find_all('a')
for i in a:
if (i.get('class') == 'xxx'):
url = i.get('href')
return url
return None
The above code has the following functions:
- converts the request to BS4 format and stores it in HTML to find all the A tags in HTML, traverse the A tag, and check whether the class value of the current A tag is equal to ‘XXX’. True will return the URL of the current A tag. If the traverse fails to match, it will return None
Read More:
- Beautiful soup gets the SRC of the picture in the page
- There is no getter for property named ‘id‘ in ‘class java.lang.Integer‘
- XML tag has empty body less… (Ctrl+F1) Reports empty tag body. The validation works in XML / JSP
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- A the tag is grayed out and cannot be clicked
- The shell runs the class file and imports the jar package that the class depends on
- Error c2011: “a certain class”: redefinition of “class” type
- OTS parsing error: invalid version tag
- The differences between the equals method in the string class and the equals method in the object class
- Solve the problem that the value in the disabled tag cannot be passed to the server
- IMG tag onerror event resolves infinite loop error reporting
- There is an error when idea creates a child item parent tag
- How to increase the effect of onchange event after adding readonly attribute to input tag and display time of laydate plug-in
- Problems encountered after Maven installation or upgrade: could not find or load main class org.codehaus.plexus .class…..
- Error: (49,15) error: tag can access private in fragmentactivity
- Ima tag error HTML, microsoft.mshtml.dll add reference and type selection error solution
- After JQ gets the tag element itself, it gets its own HTML text format (find only finds one level of child elements)
- ERROR: ../tSafe/coreReadArchive.cpp (38) – Serialization Error in verifyHeader: 0 (Version tag does
- JQuery determines whether a property has hasattr