Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use BeautifulSoup in python3 crawler

Shulou Source: shulou.com Published: 2022-06-03 17:23:23 10月02日 Update

Editor to share with you how to use BeautifulSoup in the python3 crawler, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Some friends find it too troublesome to use regular expressions to extract information.

There is an efficient web parsing library, which is called BeautifulSoup

It supports parsing in multiple formats, and we often use lxml parsers.

Case study

Html_doc = "" learn the correct posture of python, the story of handsome b

One day, Xiao Shuai b wanted to tell you two jokes: a long joke and a short joke. He asked everyone, do you want to hear the long one or the short one?

...

Import the library, transfer the source code of html to BeautifulSoupfrom bs4 import BeautifulSoupsoup=BeautifulSoup (html_doc, "lxml") print title tag content print (soup.title.string) print p tag content, print only the first print (soup.p.string) print title tag parent label print (soup.title.parent.name) print a label Only the first print (soup.a) prints the label id as link2 using the soup.find method print (soup.find (id= "link2")) getText to get the whole content print (soup.getText) select method in addition to the find method, if you are familiar with css, you can also use the select method to print the title label print (soup.select ("title") # print body label print (soup.select ("body a")) print p label The print whose id is link1 (soup.select ("p # link1")) is all the content of the article "how to use BeautifulSoup in python3 Crawler" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Tags content methods articles learning crawlers not much two information names multiple most gestures examples stories more friends formats rules source code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Huawei Microsoft OPPO Reno Redmi