python 파충류 beautifulsoup 분석 html 방법

BeautifulSoup 으로 html 와 xml 문자열 을 분석 합 니 다.
对象参数说明
실례:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import re

#      
html_doc = """
<html>
<head>
  <title>The Dormouse's story</title>
</head>
<body>
<p class="title aq">
  <b>
    The Dormouse's story
  </b>
</p>

<p class="story">Once upon a time there were three little sisters; and their names were
  <a href="http://example.com/elsie" rel="external nofollow" class="sister" id="link1">Elsie</a>,
  <a href="http://example.com/lacie" rel="external nofollow" class="sister" id="link2">Lacie</a> 
  and
  <a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>;
  and they lived at the bottom of a well.
</p>

<p class="story">...</p>
"""


# html     BeautifulSoup  
soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')

#      title   
print soup.title

#      title        
print soup.title.name

#      title        
print soup.title.string

#      title            
print soup.title.parent.name

#      p   
print soup.p

#      p     class     
print soup.p['class']

#      a     href     
print soup.a['href']
'''
soup        ,     .     , soup            
'''
#      a    href    http://www.baidu.com/
soup.a['href'] = 'http://www.baidu.com/'

#     a      name   
soup.a['name'] = u'  '

#      a     class    
del soup.a['class']

##      p         
print soup.p.contents

#      a   
print soup.a

#      a   ,       
print soup.find_all('a')

#      id      link3   a   
print soup.find(id="link3")

#        
print(soup.get_text())

#      a          
print soup.a.attrs


for link in soup.find_all('a'):
  #   link   href     
  print(link.get('href'))

# soup.p            
for child in soup.p.children:
  print(child)

#    ,     b   
for tag in soup.find_all(re.compile("b")):
  print(tag.name)
파충류 디자인 아이디어:
爬虫设计思路
자세 한 매 뉴 얼:
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
python 파충류 beautifulsoup 해석 html 방법 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 beautifulsoup 해석 html 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기