Python crawler Tutorials -25-Data Extraction-BEAUTIFULSOUP4 (iii)

Source: Internet
Author: User
Tags tag name

Python crawler Tutorials -25-Data Extraction-BEAUTIFULSOUP4 (iii)

This article describes the CSS selectors in BeautifulSoup

CSS Selector
    • Use Soup.select to return a list
    • By tag name: Soup.select ("title")
    • By class Name: Soup.select (". Centent")
    • ID Lookup: Soup.select ("#name_id")
    • Combination lookup: Soup.select ("div #input_content")
    • Property Lookup: Soup.select ("img[class= ' photo ']")
    • Get tag content: Tag.get_text

      Case
    • Code 27bs5.py File: https://xpwi.github.io/py/py%E7%88%AC%E8%99%AB/py27bs5.py
# BeautifulSoup 的使用案例# css 选择器from urllib import requestfrom bs4 import BeautifulSoupurl = ‘http://www.baidu.com/‘rsp = request.urlopen(url)content = rsp.read()soup = BeautifulSoup(content, ‘lxml‘)# bs 自动解码content = soup.prettify()print("=="*12)titles = soup.select("title")print(titles[0])print("=="*12)meta = soup.select("meta[content=‘always‘]")print(meta[0])
Run results


-This note does not allow any person or organization to reprint

Python crawler Tutorials -25-Data Extraction-BEAUTIFULSOUP4 (iii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.