Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use BeautifulSoup in python3 crawler

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report