{"id":1557,"date":"2023-03-25T11:26:10","date_gmt":"2023-03-25T03:26:10","guid":{"rendered":""},"modified":"2023-03-25T11:26:10","modified_gmt":"2023-03-25T03:26:10","slug":"Python\u722c\u866bBeautiful Soup","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1557.html","title":{"rendered":"Python\u722c\u866bBeautiful Soup"},"content":{"rendered":"
\n
Python\u722c\u866bBeautiful Soup\u8be6\u7ec6\u6559\u7a0b<\/span>\n <\/div>\n Tag<\/span>Beautiful Soup\u7684\u7b80\u4ecb<\/h2>\n
\n
\u53ef\u4ee5\u81ea\u52a8\u8f6c\u6362Unicode\u7f16\u7801\u4e3autf-8\u7f16\u7801\uff0c\u548clxml\u3001html6lib\u4e00\u6837\uff0c\u90fd\u662fpython\u51fa\u8272\u7684\u89e3\u91ca\u5668\u3002\n <\/div>\nBeautiful Soup \u5b89\u88c5<\/h2>\n
easy_install beautifulsoup4
\u6216\u8005
pip install beautifulsoup4
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n https:\/\/pypi.org\/search\/?q=beautifulsoup4\n <\/div>\nsudo python setup.py install<\/code><\/pre>\n<\/p><\/div>\n
pip install lxml<\/code><\/pre>\n<\/p><\/div>\n
pip install html5lib<\/code><\/pre>\n<\/p><\/div>\n
\n\n
\n \u89e3\u6790\u5668<\/td>\n \u4f7f\u7528\u65b9\u6cd5<\/td>\n \u4f18\u52bf<\/td>\n \u52a3\u52bf<\/td>\n<\/tr>\n \n Python\u6807\u51c6\u5e93<\/td>\n BeautifulSoup(markup, \u201chtml.parser\u201d)<\/td>\n Python\u7684\u5185\u7f6e\u6807\u51c6\u5e93
\u6267\u884c\u901f\u5ea6\u9002\u4e2d
\u6587\u6863\u5bb9\u9519\u80fd\u529b\u5f3a <\/td>\n Python\u4f4e\u7248\u672c\u4e2d\u6587\u6863\u5bb9\u9519\u80fd\u529b\u5dee <\/td>\n<\/tr>\n \n lxml HTML\u89e3\u6790\u5668<\/td>\n BeautifulSoup(markup, \u201clxml\u201d)<\/td>\n \u901f\u5ea6\u5feb
\u6587\u6863\u5bb9\u9519\u80fd\u529b\u5f3a <\/td>\n \u9700\u8981\u5b89\u88c5C\u8bed\u8a00\u5e93 <\/td>\n<\/tr>\n \n lxml XML\u89e3\u6790\u5668<\/td>\n BeautifulSoup(markup, [\u201clxml\u201d, \u201cxml\u201d])BeautifulSoup(markup, \u201cxml\u201d)<\/td>\n \u901f\u5ea6\u5feb
\u552f\u4e00\u652f\u6301XML\u7684\u89e3\u6790\u5668 <\/td>\n \u9700\u8981\u5b89\u88c5C\u8bed\u8a00\u5e93 <\/td>\n<\/tr>\n \n html5lib<\/td>\n BeautifulSoup(markup, \u201chtml5lib\u201d)<\/td>\n \u6700\u597d\u7684\u5bb9\u9519\u6027
\u6d4f\u89c8\u5668\u65b9\u5f0f\u89e3\u6790\u6587\u6863
\u751f\u6210HTML5\u683c\u5f0f\u6587\u6863 <\/td>\n \u901f\u5ea6\u6162
\u4e0d\u4f9d\u8d56\u5916\u90e8\u6269\u5c55 <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u521b\u5efaBeautiful Soup<\/h2>\n
# \u5bfc\u5165 bs4 \u5e93
<\/span> from <\/span>bs4 import <\/span>BeautifulSoup
# \u521b\u5efa beautifulsoup \u5bf9\u8c61
<\/span> soup = BeautifulSoup(html)
# \u683c\u5f0f\u5316\u8f93\u51fasoup\u5bf9\u8c61\u7684\u5185\u5bb9
<\/span> print soup.prettify<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\nBeautiful Soup 4\u79cd\u5bf9\u8c61<\/h2>\n
\n NavigableString<\/span>
\n BeautifulSoup<\/span>
\n Comment<\/span> <\/p>\nTag<\/h3>\n
\n
Beautiful Soup\u83b7\u53d6\u5185\u5bb9\u65b9\u5f0f\u5982\u4e0b\uff1a\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
# \u6bd4\u5982\uff1aTag\u662f<h1>Python\u722c\u866bBeautiful Soup<\/h1>
<\/span> # \u5bfc\u5165 bs4 \u5e93
<\/span> from <\/span>bs4 import <\/span>BeautifulSoup
# \u521b\u5efa beautifulsoup \u5bf9\u8c61
<\/span> soup = BeautifulSoup(html)
# \u8f93\u51fah1\u5bf9\u8c61\u7684\u5185\u5bb9
<\/span> print(soup.h1)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
\u5bf9\u4e8e Tag\uff0c\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u5c5e\u6027\uff0c\u662f name \u548c attrs\u3002\n <\/div>\n print soup.name
print soup.head.name
#[document]
<\/span> #head
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n print(soup.p.attrs)
#{'class'<\/span>: ['title'<\/span>], 'name'<\/span>: 'dromouse'<\/span>}
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\nprint soup.p['class'] \n
\n# ['title']<\/span><\/code><\/pre>\n<\/p><\/div>\nprint soup.p.get('class') \n
\n# ['title']<\/span><\/code><\/pre>\n<\/p><\/div>\nsoup.p['class']=\"newClass\" \n
print soup.p \n
\n# <p class=\"newClass\" name=\"dromouse\"><b>The Dormouse's story<\/b><\/p><\/span><\/code><\/pre>\n<\/p><\/div>\ndel soup.p['class'] \n
print soup.p \n
\n# <p name=\"dromouse\"><b>The Dormouse's story<\/b><\/p><\/span><\/code><\/pre>\n<\/p><\/div>\nNavigableString<\/h3>\n
print soup.p.string \n
\n# The Dormouse's story<\/span><\/code><\/pre>\n<\/p><\/div>\nBeautifulSoup<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
print type(soup.name)
# <type 'unicode'<\/span>>
<\/span> print soup.name
# [document]
<\/span> print soup.attrs
# {} \u7a7a\u5b57\u5178
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\nComment<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
print soup.a
print soup.a.string
print type(soup.a.string)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u904d\u5386\u6587\u6863\u6811<\/h2>\n
\u76f4\u63a5\u5b50\u8282\u70b9<\/h3>\n
print soup.head.contents
# [<title>Hello lidihuo<\/title>]
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n print soup.head.contents[0]
# <title>Hello lidihuo<\/title>
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n print soup.head.children
# <listiterator object at 0x7f71457f5710><\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n for <\/span>child in <\/span> soup.body.children:
print child
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u6240\u6709\u5b50\u5b59\u8282\u70b9<\/h3>\n
for <\/span>child \n in <\/span> soup.body.children: \n
print child \n
<\/code><\/pre>\n<\/p><\/div>\n\u8282\u70b9\u5185\u5bb9<\/h3>\n
\n
\u901a\u4fd7\u70b9\u8bf4\u5c31\u662f\uff1a\u5982\u679c\u4e00\u4e2a\u6807\u7b7e\u91cc\u9762\u6ca1\u6709\u6807\u7b7e\u4e86\uff0c\u90a3\u4e48 .string \u5c31\u4f1a\u8fd4\u56de\u6807\u7b7e\u91cc\u9762\u7684\u5185\u5bb9\u3002\u5982\u679c\u6807\u7b7e\u91cc\u9762\u53ea\u6709\u552f\u4e00\u7684\u4e00\u4e2a\u6807\u7b7e\u4e86\uff0c\u90a3\u4e48 .string \u4e5f\u4f1a\u8fd4\u56de\u6700\u91cc\u9762\u7684\u5185\u5bb9\u3002\u4f8b\u5982\n <\/div>\nprint soup.head.string \n
\n# The Dormouse's story
<\/span> print soup.title.string \n
\n# The Dormouse's story<\/span><\/code><\/pre>\n<\/p><\/div>\nprint soup.html.string \n
\n# None<\/span><\/code><\/pre>\n<\/p><\/div>\n\u591a\u4e2a\u5185\u5bb9<\/h3>\n
for string in soup.strings: \n
print(repr(string))<\/code><\/pre>\n<\/p><\/div>\nfor string in soup.stripped_strings: \n
print(repr(string))<\/code><\/pre>\n<\/p><\/div>\n\u7236\u8282\u70b9<\/h3>\n
content = soup.head.title.string \n
for parent in content.parents: \n
print parent.name<\/code><\/pre>\n<\/p><\/div>\n\u5144\u5f1f\u8282\u70b9<\/h3>\n
\n
\u6ce8\u610f\uff1a\u5b9e\u9645\u6587\u6863\u4e2d\u7684tag\u7684 .next_sibling \u548c .previous_sibling \u5c5e\u6027\u901a\u5e38\u662f\u5b57\u7b26\u4e32\u6216\u7a7a\u767d\uff0c\u56e0\u4e3a\u7a7a\u767d\u6216\u8005\u6362\u884c\u4e5f\u53ef\u4ee5\u88ab\u89c6\u4f5c\u4e00\u4e2a\u8282\u70b9\uff0c\u6240\u4ee5\u5f97\u5230\u7684\u7ed3\u679c\u53ef\u80fd\u662f\u7a7a\u767d\u6216\u8005\u6362\u884c\n <\/div>\nprint soup.p.next_sibling \n
\n# \u5b9e\u9645\u8be5\u5904\u4e3a\u7a7a\u767d<\/span> \n
print soup.p.prev_sibling \n
\n# None\u6ca1\u6709\u524d\u4e00\u4e2a\u5144\u5f1f\u8282\u70b9\uff0c\u8fd4\u56de None<\/span> \n
print soup.p.next_sibling.next_sibling \n
<\/code><\/pre>\n<\/p><\/div>\n\u5168\u90e8\u5144\u5f1f\u8282\u70b9<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
for <\/span>sibling in <\/span>soup.a.next_siblings:
print(repr(sibling))
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u524d\u540e\u8282\u70b9<\/h3>\n
<head><title>Hello Lidihuo<\/title><\/head><\/code><\/pre>\n<\/p><\/div>\n
print soup.head.next_element \n
\n# <title>The Dormouse's story<\/title><\/span><\/code><\/pre>\n<\/p><\/div>\n\u6240\u6709\u524d\u540e\u8282\u70b9<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
for <\/span>element in <\/span>last_a_tag.next_elements:
print(repr(element))
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u641c\u7d22\u6587\u6863\u6811<\/h2>\n
find_all(name , attrs , recursive , text , **kwargs)<\/h3>\n
\n
name \u53c2\u6570\u53ef\u4ee5\u67e5\u627e\u6240\u6709\u540d\u5b57\u4e3a name \u7684tag,\u5b57\u7b26\u4e32\u5bf9\u8c61\u4f1a\u88ab\u81ea\u52a8\u5ffd\u7565\u6389\n <\/div>\n
\n \u6807\u7b7e\u3002<\/b>\n <\/div>\nsoup.find_all('b')<\/code><\/pre>\n<\/p><\/div>\n
soup.find_all([\"a\", \"b\"])<\/code><\/pre>\n<\/p><\/div>\n