{"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 <\/head>
\n <\/p>\n

\n

Python\u722c\u866bBeautiful Soup<\/h1>\n

Python\u722c\u866bBeautiful Soup\u8be6\u7ec6\u6559\u7a0b<\/span>\n <\/div>\n

Beautiful Soup\u7684\u7b80\u4ecb<\/h2>\n
\n Beautiful Soup\u662fpython\u7684\u4e00\u4e2a\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u4ece\u7f51\u9875\u4e0a\u722c\u53d6\u6570\u636e\u3002
\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>\n

Beautiful Soup \u5b89\u88c5<\/h2>\n
\n \u4f7f\u7528\u4e4b\u524d\uff0c\u6211\u4eec\u5f53\u7136\u9700\u8981\u9996\u5148\u8bf4\u660e\u4e00\u4e0b\u5b83\u7684\u5b89\u88c5\u65b9\u5f0f\u3002\u76ee\u524dBeautifulSoup\u7684\u6700\u65b0\u7248\u672c\u662f4.x\u7248\u672c\uff0c\u4e4b\u524d\u7684\u7248\u672c\u5df2\u7ecf\u505c\u6b62\u5f00\u53d1\u4e86\uff0c\u63a8\u8350\u4f7f\u7528pip\/easy_install\u6765\u5b89\u88c5\uff0c\u5b89\u88c5\u547d\u4ee4\u5982\u4e0b\uff1a\n <\/div>\n
\n
 easy_install beautifulsoup4
\u6216\u8005
pip install beautifulsoup4
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5982\u679c\u60f3\u5b89\u88c5\u6700\u65b0\u7684\u7248\u672c\uff0c\u8bf7\u76f4\u63a5\u4e0b\u8f7d\u5b89\u88c5\u5305\u6765\u624b\u52a8\u5b89\u88c5\uff0c\u4e5f\u662f\u5341\u5206\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002
\n https:\/\/pypi.org\/search\/?q=beautifulsoup4\n <\/div>\n
\n \u4e0b\u8f7d\u5b8c\u6210\u4e4b\u540e\u89e3\u538b\uff0c\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u5373\u53ef\u5b8c\u6210\u5b89\u88c5\n <\/div>\n
\n
sudo python setup.py install<\/code><\/pre>\n<\/p><\/div>\n
\n \u540c\u65f6\uff0c\u8fd8\u9700\u8981\u5b89\u88c5 lxml\n <\/div>\n
\n
pip install lxml<\/code><\/pre>\n<\/p><\/div>\n
\n \u53e6\u4e00\u4e2a\u53ef\u4f9b\u9009\u62e9\u7684\u89e3\u6790\u5668\u662f\u7eafPython\u5b9e\u73b0\u7684 html5lib , html5lib\u7684\u89e3\u6790\u65b9\u5f0f\u4e0e\u6d4f\u89c8\u5668\u76f8\u540c,\u53ef\u4ee5\u9009\u62e9\u4e0b\u5217\u65b9\u6cd5\u6765\u5b89\u88c5html5lib:\n <\/div>\n
\n
pip install html5lib<\/code><\/pre>\n<\/p><\/div>\n
\n Beautiful Soup\u652f\u6301Python\u6807\u51c6\u5e93\u4e2d\u7684HTML\u89e3\u6790\u5668,\u8fd8\u652f\u6301\u4e00\u4e9b\u7b2c\u4e09\u65b9\u7684\u89e3\u6790\u5668\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u5b89\u88c5\u5b83\uff0c\u5219 Python \u4f1a\u4f7f\u7528 Python\u9ed8\u8ba4\u7684\u89e3\u6790\u5668\uff0clxml \u89e3\u6790\u5668\u66f4\u52a0\u5f3a\u5927\uff0c\u901f\u5ea6\u66f4\u5feb\uff0c\u63a8\u8350\u5b89\u88c5\u3002\n <\/div>\n\n\n\n\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
Python\u6807\u51c6\u5e93<\/td>\nBeautifulSoup(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
lxml HTML\u89e3\u6790\u5668<\/td>\nBeautifulSoup(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
lxml XML\u89e3\u6790\u5668<\/td>\nBeautifulSoup(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
html5lib<\/td>\nBeautifulSoup(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
\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>\n

Beautiful Soup 4\u79cd\u5bf9\u8c61<\/h2>\n
\n Beautiful Soup\u5c06\u590d\u6742HTML\u6587\u6863\u8f6c\u6362\u6210\u4e00\u4e2a\u590d\u6742\u7684\u6811\u5f62\u7ed3\u6784,\u6bcf\u4e2a\u8282\u70b9\u90fd\u662fPython\u5bf9\u8c61,\u6240\u6709\u5bf9\u8c61\u53ef\u4ee5\u5f52\u7eb3\u4e3a4\u79cd:\n <\/div>\n

Tag<\/span>
\n NavigableString<\/span>
\n BeautifulSoup<\/span>
\n Comment<\/span> <\/p>\n

\n \u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u770b\u770b\n <\/div>\n

Tag<\/h3>\n
\n Tag\u901a\u4fd7\u70b9\u8bb2\u5c31\u662fHTML\u4e2d\u7684\u6807\u7b7e\uff0c\u4f8b\u5982<title>\u3001<div><a><img>\u7b49\u7b49\u3002
\n
Beautiful Soup\u83b7\u53d6\u5185\u5bb9\u65b9\u5f0f\u5982\u4e0b\uff1a\n <\/div>\n
\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 \u5229\u7528soup\u52a0\u6807\u7b7e\u540d\u8f7b\u677e\u5730\u83b7\u53d6\u8fd9\u4e9b\u6807\u7b7e\u7684\u5185\u5bb9\u3002
\n
\u5bf9\u4e8e Tag\uff0c\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u5c5e\u6027\uff0c\u662f name \u548c attrs\u3002\n <\/div>\n
\n name\n <\/div>\n
\n
 print soup.name
print soup.head.name
#[document]
<\/span> #head
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n soup \u5bf9\u8c61\u672c\u8eab\u6bd4\u8f83\u7279\u6b8a\uff0c\u5b83\u7684 name \u5373\u4e3a [document]\uff0c\u5bf9\u4e8e\u5176\u4ed6\u5185\u90e8\u6807\u7b7e\uff0c\u8f93\u51fa\u7684\u503c\u4fbf\u4e3a\u6807\u7b7e\u672c\u8eab\u7684\u540d\u79f0\u3002\n <\/div>\n
\n attrs\n <\/div>\n
\n
 print(soup.p.attrs)
#{'class'<\/span>: ['title'<\/span>], 'name'<\/span>: 'dromouse'<\/span>}
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u628a p \u6807\u7b7e\u7684\u6240\u6709\u5c5e\u6027\u6253\u5370\u8f93\u51fa\u4e86\u51fa\u6765\uff0c\u5f97\u5230\u7684\u7c7b\u578b\u662f\u4e00\u4e2a\u5b57\u5178\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5355\u72ec\u83b7\u53d6\u67d0\u4e2a\u5c5e\u6027\uff0c\u53ef\u4ee5\u8fd9\u6837\uff0c\u4f8b\u5982\u6211\u4eec\u83b7\u53d6\u5b83\u7684 class \u53eb\u4ec0\u4e48\n <\/div>\n
\n
print soup.p['class'] \n
\n# ['title']<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd8\u53ef\u4ee5\u8fd9\u6837\uff0c\u5229\u7528get\u65b9\u6cd5\uff0c\u4f20\u5165\u5c5e\u6027\u7684\u540d\u79f0\uff0c\u4e8c\u8005\u662f\u7b49\u4ef7\u7684\n <\/div>\n
\n
print soup.p.get('class') \n
\n# ['title']<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6211\u4eec\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u5c5e\u6027\u548c\u5185\u5bb9\u7b49\u7b49\u8fdb\u884c\u4fee\u6539\uff0c\u4f8b\u5982\n <\/div>\n
\n
soup.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>\n
\n \u8fd8\u53ef\u4ee5\u5bf9\u8fd9\u4e2a\u5c5e\u6027\u8fdb\u884c\u5220\u9664\uff0c\u4f8b\u5982\n <\/div>\n
\n
del 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>\n

NavigableString<\/h3>\n
\n \u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u4e86\u6807\u7b7e\u7684\u5185\u5bb9\uff0c\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u6211\u4eec\u8981\u60f3\u83b7\u53d6\u6807\u7b7e\u5185\u90e8\u7684\u6587\u5b57\u600e\u4e48\u529e\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u7528 .string \u5373\u53ef\uff0c\u4f8b\u5982\n <\/div>\n
\n
print soup.p.string \n
\n# The Dormouse's story<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd9\u6837\u6211\u4eec\u5c31\u8f7b\u677e\u83b7\u53d6\u5230\u4e86\u6807\u7b7e\u91cc\u9762\u7684\u5185\u5bb9\u3002\n <\/div>\n

BeautifulSoup<\/h3>\n
\n BeautifulSoup \u5bf9\u8c61\u8868\u793a\u7684\u662f\u4e00\u4e2a\u6587\u6863\u7684\u5168\u90e8\u5185\u5bb9.\u5927\u90e8\u5206\u65f6\u5019,\u53ef\u4ee5\u628a\u5b83\u5f53\u4f5c Tag \u5bf9\u8c61\uff0c\u662f\u4e00\u4e2a\u7279\u6b8a\u7684 Tag\uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u5b83\u7684\u7c7b\u578b\uff0c\u540d\u79f0\u3002\n <\/div>\n
\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>\n

Comment<\/h3>\n
\n Comment \u5bf9\u8c61\u662f\u4e00\u4e2a\u7279\u6b8a\u7c7b\u578b\u7684 NavigableString \u5bf9\u8c61\uff0c\u5176\u5b9e\u8f93\u51fa\u7684\u5185\u5bb9\u4ecd\u7136\u4e0d\u5305\u62ec\u6ce8\u91ca\u7b26\u53f7\uff0c\u4f46\u662f\u5982\u679c\u4e0d\u597d\u597d\u5904\u7406\u5b83\uff0c\u53ef\u80fd\u4f1a\u5bf9\u6211\u4eec\u7684\u6587\u672c\u5904\u7406\u9020\u6210\u610f\u60f3\u4e0d\u5230\u7684\u9ebb\u70e6\u3002\u6211\u4eec\u627e\u4e00\u4e2a\u5e26\u6ce8\u91ca\u7684\u6807\u7b7e\n <\/div>\n
\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
\n .contents\n <\/div>\n
\n tag \u7684 .content \u5c5e\u6027\u53ef\u4ee5\u5c06tag\u7684\u5b50\u8282\u70b9\u4ee5\u5217\u8868\u7684\u65b9\u5f0f\u8f93\u51fa\n <\/div>\n
\n
 print soup.head.contents 
# [<title>Hello lidihuo<\/title>]
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa\u65b9\u5f0f\u4e3a\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5217\u8868\u7d22\u5f15\u6765\u83b7\u53d6\u5b83\u7684\u67d0\u4e00\u4e2a\u5143\u7d20\n <\/div>\n
\n
 print soup.head.contents[0]
# <title>Hello lidihuo<\/title>
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n .children\n <\/div>\n
\n \u5b83\u8fd4\u56de\u7684\u4e0d\u662f\u4e00\u4e2a list\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u83b7\u53d6\u6240\u6709\u5b50\u8282\u70b9\u3002\u6211\u4eec\u6253\u5370\u8f93\u51fa .children \u770b\u4e00\u4e0b\uff0c\u53ef\u4ee5\u53d1\u73b0\u5b83\u662f\u4e00\u4e2a list \u751f\u6210\u5668\u5bf9\u8c61\n <\/div>\n
\n
 print soup.head.children
# <listiterator object at 0x7f71457f5710><\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u83b7\u5f97\u91cc\u9762\u7684\u5185\u5bb9\u5462\u904d\u5386\u4e00\u4e0b\u5c31\u597d\u4e86\u4ee3\u7801\u5982\u4e0b:\n <\/div>\n
\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
\n .descendants\n <\/div>\n
\n .contents \u548c .children \u5c5e\u6027\u4ec5\u5305\u542btag\u7684\u76f4\u63a5\u5b50\u8282\u70b9\uff0c.descendants \u5c5e\u6027\u53ef\u4ee5\u5bf9\u6240\u6709tag\u7684\u5b50\u5b59\u8282\u70b9\u8fdb\u884c\u9012\u5f52\u5faa\u73af\uff0c\u548c children\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u904d\u5386\u83b7\u53d6\u5176\u4e2d\u7684\u5185\u5bb9\u3002\n <\/div>\n
\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 \u5982\u679ctag\u53ea\u6709\u4e00\u4e2a NavigableString \u7c7b\u578b\u5b50\u8282\u70b9,\u90a3\u4e48\u8fd9\u4e2atag\u53ef\u4ee5\u4f7f\u7528 .string \u5f97\u5230\u5b50\u8282\u70b9\u3002\u5982\u679c\u4e00\u4e2atag\u4ec5\u6709\u4e00\u4e2a\u5b50\u8282\u70b9,\u90a3\u4e48\u8fd9\u4e2atag\u4e5f\u53ef\u4ee5\u4f7f\u7528 .string \u65b9\u6cd5,\u8f93\u51fa\u7ed3\u679c\u4e0e\u5f53\u524d\u552f\u4e00\u5b50\u8282\u70b9\u7684 .string \u7ed3\u679c\u76f8\u540c\u3002
\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>\n
\n
print 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>\n
\n \u5982\u679ctag\u5305\u542b\u4e86\u591a\u4e2a\u5b50\u8282\u70b9,tag\u5c31\u65e0\u6cd5\u786e\u5b9a\uff0cstring \u65b9\u6cd5\u5e94\u8be5\u8c03\u7528\u54ea\u4e2a\u5b50\u8282\u70b9\u7684\u5185\u5bb9, .string \u7684\u8f93\u51fa\u7ed3\u679c\u662f None\n <\/div>\n
\n
print soup.html.string \n
\n# None<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u591a\u4e2a\u5185\u5bb9<\/h3>\n
\n .strings\n <\/div>\n
\n \u83b7\u53d6\u591a\u4e2a\u5185\u5bb9\uff0c\u4e0d\u8fc7\u9700\u8981\u904d\u5386\u83b7\u53d6\n <\/div>\n
\n
for string in soup.strings: \n
    print(repr(string))<\/code><\/pre>\n<\/p><\/div>\n
\n .stripped_strings\n <\/div>\n
\n \u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u4e2d\u53ef\u80fd\u5305\u542b\u4e86\u5f88\u591a\u7a7a\u683c\u6216\u7a7a\u884c,\u4f7f\u7528 .stripped_strings \u53ef\u4ee5\u53bb\u9664\u591a\u4f59\u7a7a\u767d\u5185\u5bb9\u3002\n <\/div>\n
\n
for string in soup.stripped_strings: \n
    print(repr(string))<\/code><\/pre>\n<\/p><\/div>\n

\u7236\u8282\u70b9<\/h3>\n
\n \u901a\u8fc7\u5143\u7d20\u7684 .parents \u5c5e\u6027\u53ef\u4ee5\u9012\u5f52\u5f97\u5230\u5143\u7d20\u7684\u6240\u6709\u7236\u8f88\u8282\u70b9\uff0c\u4f8b\u5982\n <\/div>\n
\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 \u5144\u5f1f\u8282\u70b9\u53ef\u4ee5\u7406\u89e3\u4e3a\u548c\u672c\u8282\u70b9\u5904\u5728\u7edf\u4e00\u7ea7\u7684\u8282\u70b9\uff0c.next_sibling \u5c5e\u6027\u83b7\u53d6\u4e86\u8be5\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u5144\u5f1f\u8282\u70b9\uff0c.previous_sibling \u5219\u4e0e\u4e4b\u76f8\u53cd\uff0c\u5982\u679c\u8282\u70b9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de None
\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>\n
\n
print 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
\n \u901a\u8fc7 .next_siblings \u548c .previous_siblings \u5c5e\u6027\u53ef\u4ee5\u5bf9\u5f53\u524d\u8282\u70b9\u7684\u5144\u5f1f\u8282\u70b9\u8fed\u4ee3\u8f93\u51fa\u3002\n <\/div>\n
\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
\n \u4e0e .next_sibling .previous_sibling \u4e0d\u540c\uff0c\u5b83\u5e76\u4e0d\u662f\u9488\u5bf9\u4e8e\u5144\u5f1f\u8282\u70b9\uff0c\u800c\u662f\u5728\u6240\u6709\u8282\u70b9\uff0c\u4e0d\u5206\u5c42\u6b21\uff0c\u6bd4\u5982 head \u8282\u70b9\u4e3a\n <\/div>\n
\n
<head><title>Hello Lidihuo<\/title><\/head><\/code><\/pre>\n<\/p><\/div>\n
\n \u90a3\u4e48\u5b83\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u4fbf\u662f title\uff0c\u5b83\u662f\u4e0d\u5206\u5c42\u6b21\u5173\u7cfb\u7684\n <\/div>\n
\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
\n \u901a\u8fc7 .next_elements \u548c .previous_elements \u7684\u8fed\u4ee3\u5668\u5c31\u53ef\u4ee5\u5411\u524d\u6216\u5411\u540e\u8bbf\u95ee\u6587\u6863\u7684\u89e3\u6790\u5185\u5bb9,\u5c31\u597d\u50cf\u6587\u6863\u6b63\u5728\u88ab\u89e3\u6790\u4e00\u6837\u3002\n <\/div>\n
\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 find_all() \u65b9\u6cd5\u641c\u7d22\u5f53\u524dtag\u7684\u6240\u6709tag\u5b50\u8282\u70b9,\u5e76\u5224\u65ad\u662f\u5426\u7b26\u5408\u8fc7\u6ee4\u5668\u7684\u6761\u4ef6\u3002
\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 \u6700\u7b80\u5355\u7684\u8fc7\u6ee4\u5668\u662f\u5b57\u7b26\u4e32.\u5728\u641c\u7d22\u65b9\u6cd5\u4e2d\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570,Beautiful Soup\u4f1a\u67e5\u627e\u4e0e\u5b57\u7b26\u4e32\u5b8c\u6574\u5339\u914d\u7684\u5185\u5bb9,\u4e0b\u9762\u7684\u4f8b\u5b50\u7528\u4e8e\u67e5\u627e\u6587\u6863\u4e2d\u6240\u6709\u7684
\n \u6807\u7b7e\u3002<\/b>\n <\/div>\n
\n
soup.find_all('b')<\/code><\/pre>\n<\/p><\/div>\n
\n \u5982\u679c\u4f20\u5165\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570,Beautiful Soup\u4f1a\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u7684 match() \u6765\u5339\u914d\u5185\u5bb9.\u4e0b\u9762\u4f8b\u5b50\u4e2d\u627e\u51fa\u6240\u6709\u4ee5b\u5f00\u5934\u7684\u6807\u7b7e,\u8fd9\u8868\u793a<body>\u548c<b>\u6807\u7b7e\u90fd\u5e94\u8be5\u88ab\u627e\u5230\u3002\n <\/div>\n
\n \u5982\u679c\u4f20\u5165\u5217\u8868\u53c2\u6570,Beautiful Soup\u4f1a\u5c06\u4e0e\u5217\u8868\u4e2d\u4efb\u4e00\u5143\u7d20\u5339\u914d\u7684\u5185\u5bb9\u8fd4\u56de.\u4e0b\u9762\u4ee3\u7801\u627e\u5230\u6587\u6863\u4e2d\u6240\u6709<a>\u6807\u7b7e\u548c<b>\u6807\u7b7e\n <\/div>\n
\n
soup.find_all([\"a\", \"b\"])<\/code><\/pre>\n<\/p><\/div>\n
\n True \u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u503c,\u4e0b\u9762\u4ee3\u7801\u67e5\u627e\u5230\u6240\u6709\u7684tag,\u4f46\u662f\u4e0d\u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32\u8282\u70b9\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
for <\/span>tag in <\/span>soup.find_all<\/span>(True):
    print(tag.name)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5982\u679c\u6ca1\u6709\u5408\u9002\u8fc7\u6ee4\u5668,\u90a3\u4e48\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5,\u65b9\u6cd5\u53ea\u63a5\u53d7\u4e00\u4e2a\u5143\u7d20\u53c2\u6570 [4] ,\u5982\u679c\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de True \u8868\u793a\u5f53\u524d\u5143\u7d20\u5339\u914d\u5e76\u4e14\u88ab\u627e\u5230,\u5982\u679c\u4e0d\u662f\u5219\u53cd\u56de False
\n
\u4e0b\u9762\u65b9\u6cd5\u6821\u9a8c\u4e86\u5f53\u524d\u5143\u7d20,\u5982\u679c\u5305\u542b class \u5c5e\u6027\u5374\u4e0d\u5305\u542b id \u5c5e\u6027,\u90a3\u4e48\u5c06\u8fd4\u56de True:\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
def <\/span>has_class_but_no_id(tag):
    return <\/span>tag.has_attr<\/span>('class'<\/span>) and <\/span>not tag.has_attr<\/span>('id'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5c06\u8fd9\u4e2a\u65b9\u6cd5\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165 find_all() \u65b9\u6cd5,\u5c06\u5f97\u5230\u6240\u6709<p>\u6807\u7b7e: <\/p>\n<\/p><\/div>\n
\n
soup.find_all(has_class_but_no_id)<\/code><\/pre>\n<\/p><\/div>\n

keyword \u53c2\u6570<\/h3>\n
\n \u5982\u679c\u4e00\u4e2a\u6307\u5b9a\u540d\u5b57\u7684\u53c2\u6570\u4e0d\u662f\u641c\u7d22\u5185\u7f6e\u7684\u53c2\u6570\u540d,\u641c\u7d22\u65f6\u4f1a\u628a\u8be5\u53c2\u6570\u5f53\u4f5c\u6307\u5b9a\u540d\u5b57tag\u7684\u5c5e\u6027\u6765\u641c\u7d22,\u5982\u679c\u5305\u542b\u4e00\u4e2a\u540d\u5b57\u4e3a id \u7684\u53c2\u6570,Beautiful Soup\u4f1a\u641c\u7d22\u6bcf\u4e2atag\u7684\u201did\u201d\u5c5e\u6027\u3002\n <\/div>\n
\n
soup.find_all(id='link2')<\/code><\/pre>\n<\/p><\/div>\n
\n \u5982\u679c\u4f20\u5165 href \u53c2\u6570,Beautiful Soup\u4f1a\u641c\u7d22\u6bcf\u4e2atag\u7684\u201dhref\u201d\u5c5e\u6027\u3002\n <\/div>\n
\n
soup.find_all(href=re.compile(\"elsie\"))<\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u8fd9\u91cc\u6211\u4eec\u60f3\u7528 class \u8fc7\u6ee4\uff0c\u4e0d\u8fc7 class \u662f python \u7684\u5173\u952e\u8bcd\uff0c\u8fd9\u600e\u4e48\u529e\uff1f\u52a0\u4e2a\u4e0b\u5212\u7ebf\u5c31\u53ef\u4ee5\u3002\n <\/div>\n
\n
soup.find_all(\"a\", class_=\"sister\")<\/code><\/pre>\n<\/p><\/div>\n
\n \u6709\u4e9btag\u5c5e\u6027\u5728\u641c\u7d22\u4e0d\u80fd\u4f7f\u7528,\u6bd4\u5982HTML5\u4e2d\u7684 data-* \u5c5e\u6027\n <\/div>\n
\n
data_soup = BeautifulSoup(' \n\n     
\n foo! \n <\/div>') data_soup.find_all(data-foo=\"value\")<\/code><\/pre>\n<\/p><\/div>\n
\n \u4f46\u662f\u53ef\u4ee5\u901a\u8fc7 find_all() \u65b9\u6cd5\u7684 attrs \u53c2\u6570\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u53c2\u6570\u6765\u641c\u7d22\u5305\u542b\u7279\u6b8a\u5c5e\u6027\u7684tag\n <\/div>\n
\n
data_soup.find_all(attrs={\"data-foo\": \"value\"})<\/code><\/pre>\n<\/p><\/div>\n

text \u53c2\u6570<\/h3>\n
\n \u901a\u8fc7 text \u53c2\u6570\u53ef\u4ee5\u641c\u641c\u6587\u6863\u4e2d\u7684\u5b57\u7b26\u4e32\u5185\u5bb9.\u4e0e name \u53c2\u6570\u7684\u53ef\u9009\u503c\u4e00\u6837, text \u53c2\u6570\u63a5\u53d7 \u5b57\u7b26\u4e32 , \u6b63\u5219\u8868\u8fbe\u5f0f , \u5217\u8868, True\n <\/div>\n
\n
soup.find_all(text=\"Elsie\") \n
soup.find_all(text=[\"Tillie\", \"Elsie\", \"Lacie\"]) \n
soup.find_all(text=re.compile(\"Dormouse\"))<\/code><\/pre>\n<\/p><\/div>\n

limit \u53c2\u6570<\/h3>\n
\n find_all() \u65b9\u6cd5\u8fd4\u56de\u5168\u90e8\u7684\u641c\u7d22\u7ed3\u6784,\u5982\u679c\u6587\u6863\u6811\u5f88\u5927\u90a3\u4e48\u641c\u7d22\u4f1a\u5f88\u6162.\u5982\u679c\u6211\u4eec\u4e0d\u9700\u8981\u5168\u90e8\u7ed3\u679c,\u53ef\u4ee5\u4f7f\u7528 limit \u53c2\u6570\u9650\u5236\u8fd4\u56de\u7ed3\u679c\u7684\u6570\u91cf.\u6548\u679c\u4e0eSQL\u4e2d\u7684limit\u5173\u952e\u5b57\u7c7b\u4f3c,\u5f53\u641c\u7d22\u5230\u7684\u7ed3\u679c\u6570\u91cf\u8fbe\u5230 limit \u7684\u9650\u5236\u65f6,\u5c31\u505c\u6b62\u641c\u7d22\u8fd4\u56de\u7ed3\u679c.
\n
\u6587\u6863\u6811\u4e2d\u67093\u4e2atag\u7b26\u5408\u641c\u7d22\u6761\u4ef6,\u4f46\u7ed3\u679c\u53ea\u8fd4\u56de\u4e862\u4e2a,\u56e0\u4e3a\u6211\u4eec\u9650\u5236\u4e86\u8fd4\u56de\u6570\u91cf\n <\/div>\n
\n
soup.find_all(\"a\", limit=2)<\/code><\/pre>\n<\/p><\/div>\n

recursive \u53c2\u6570<\/h3>\n
\n \u8c03\u7528tag\u7684 find_all() \u65b9\u6cd5\u65f6,Beautiful Soup\u4f1a\u68c0\u7d22\u5f53\u524dtag\u7684\u6240\u6709\u5b50\u5b59\u8282\u70b9,\u5982\u679c\u53ea\u60f3\u641c\u7d22tag\u7684\u76f4\u63a5\u5b50\u8282\u70b9,\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570 recursive=False .
\n
\u662f\u5426\u4f7f\u7528 recursive \u53c2\u6570\u7684\u641c\u7d22\u7ed3\u679c:\n <\/div>\n
\n
soup.html.find_all(\"title\") \n
soup.html.find_all(\"title\", recursive=False)<\/code><\/pre>\n<\/p><\/div>\n
\n find(name , attrs , recursive , text , **kwargs)<\/b>\n <\/div>\n
\n \u5b83\u4e0e find_all() \u65b9\u6cd5\u552f\u4e00\u7684\u533a\u522b\u662f find_all() \u65b9\u6cd5\u7684\u8fd4\u56de\u7ed3\u679c\u662f\u503c\u5305\u542b\u4e00\u4e2a\u5143\u7d20\u7684\u5217\u8868,\u800c find() \u65b9\u6cd5\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002\n <\/div>\n
\n find_parents() find_parent()<\/b>\n <\/div>\n
\n find_all() \u548c find() \u53ea\u641c\u7d22\u5f53\u524d\u8282\u70b9\u7684\u6240\u6709\u5b50\u8282\u70b9,\u5b59\u5b50\u8282\u70b9\u7b49. find_parents() \u548c find_parent()
\n
\u7528\u6765\u641c\u7d22\u5f53\u524d\u8282\u70b9\u7684\u7236\u8f88\u8282\u70b9,\u641c\u7d22\u65b9\u6cd5\u4e0e\u666e\u901atag\u7684\u641c\u7d22\u65b9\u6cd5\u76f8\u540c,\u641c\u7d22\u6587\u6863\u641c\u7d22\u6587\u6863\u5305\u542b\u7684\u5185\u5bb9\u3002\n <\/div>\n
\n find_next_siblings() find_next_sibling()<\/b>\n <\/div>\n
\n \u8fd92\u4e2a\u65b9\u6cd5\u901a\u8fc7 .next_siblings \u5c5e\u6027\u5bf9\u5f53 tag \u7684\u6240\u6709\u540e\u9762\u89e3\u6790\u7684\u5144\u5f1f tag \u8282\u70b9\u8fdb\u884c\u8fed\u4ee3, find_next_siblings() \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u540e\u9762\u7684\u5144\u5f1f\u8282\u70b9,find_next_sibling() \u53ea\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u540e\u9762\u7684\u7b2c\u4e00\u4e2atag\u8282\u70b9\u3002\n <\/div>\n
\n find_previous_siblings() find_previous_sibling()<\/b>\n <\/div>\n
\n \u8fd92\u4e2a\u65b9\u6cd5\u901a\u8fc7 .previous_siblings \u5c5e\u6027\u5bf9\u5f53\u524d tag \u7684\u524d\u9762\u89e3\u6790\u7684\u5144\u5f1f tag \u8282\u70b9\u8fdb\u884c\u8fed\u4ee3, find_previous_siblings() \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u524d\u9762\u7684\u5144\u5f1f\u8282\u70b9, find_previous_sibling() \u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u524d\u9762\u7684\u5144\u5f1f\u8282\u70b9\u3002\n <\/div>\n
\n find_all_next() find_next()<\/b>\n <\/div>\n
\n \u8fd92\u4e2a\u65b9\u6cd5\u901a\u8fc7 .next_elements \u5c5e\u6027\u5bf9\u5f53\u524d tag \u7684\u4e4b\u540e\u7684 tag \u548c\u5b57\u7b26\u4e32\u8fdb\u884c\u8fed\u4ee3, find_all_next() \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9, find_next() \u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9\u3002\n <\/div>\n
\n find_all_previous() \u548c find_previous()<\/b>\n <\/div>\n
\n \u8fd92\u4e2a\u65b9\u6cd5\u901a\u8fc7 .previous_elements \u5c5e\u6027\u5bf9\u5f53\u524d\u8282\u70b9\u524d\u9762\u7684 tag \u548c\u5b57\u7b26\u4e32\u8fdb\u884c\u8fed\u4ee3, find_all_previous() \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9, find_previous()\u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u8282\u70b9\u3002\n <\/div>\n

CSS\u9009\u62e9\u5668<\/h2>\n
\n \u6211\u4eec\u5728\u5199 CSS \u65f6\uff0c\u6807\u7b7e\u540d\u4e0d\u52a0\u4efb\u4f55\u4fee\u9970\uff0c\u7c7b\u540d\u524d\u52a0\u70b9\uff0cid\u540d\u524d\u52a0 #\uff0c\u5728\u8fd9\u91cc\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u7b5b\u9009\u5143\u7d20\uff0c\u7528\u5230\u7684\u65b9\u6cd5\u662f soup.select()\uff0c\u8fd4\u56de\u7c7b\u578b\u662f list\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
# \u901a\u8fc7\u6807\u7b7e\u540d\u67e5\u627e
<\/span> print soup.select<\/span>('title'<\/span>)
print soup.select<\/span>('a'<\/span>)
print soup.select<\/span>('b'<\/span>)
# \u901a\u8fc7\u7c7b\u540d\u67e5\u627e
<\/span> print soup.select<\/span>('.sister'<\/span>)
#\u901a\u8fc7 id \u540d\u67e5\u627e
<\/span> print soup.select<\/span>('#link1'<\/span>)
<\/span><\/span> # \u7ec4\u5408\u67e5\u627e,\u7ec4\u5408\u67e5\u627e\u5373\u548c\u5199 class <\/span>\u6587\u4ef6\u65f6\uff0c\u6807\u7b7e\u540d\u4e0e\u7c7b\u540d\u3001id\u540d\u8fdb\u884c\u7684\u7ec4\u5408\u539f\u7406\u662f\u4e00\u6837\u7684\uff0c\u4f8b\u5982\u67e5\u627e p \u6807\u7b7e\u4e2d\uff0cid \u7b49\u4e8e link1\u7684\u5185\u5bb9\uff0c\u4e8c\u8005\u9700\u8981\u7528\u7a7a\u683c\u5206\u5f00
<\/span> print soup.select<\/span>('p #link1'<\/span>)
# \u76f4\u63a5\u5b50\u6807\u7b7e\u67e5\u627e
<\/span> print soup.select<\/span>(\"head > title\"<\/span>)
# \u5c5e\u6027\u67e5\u627e,\u67e5\u627e\u65f6\u8fd8\u53ef\u4ee5\u52a0\u5165\u5c5e\u6027\u5143\u7d20\uff0c\u5c5e\u6027\u9700\u8981\u7528\u4e2d\u62ec\u53f7\u62ec\u8d77\u6765\uff0c\u6ce8\u610f\u5c5e\u6027\u548c\u6807\u7b7e\u5c5e\u4e8e\u540c\u4e00\u8282\u70b9\uff0c\u6240\u4ee5\u4e2d\u95f4\u4e0d\u80fd\u52a0\u7a7a\u683c\uff0c\u5426\u5219\u4f1a\u65e0\u6cd5\u5339\u914d\u5230\u3002
<\/span> print soup.select<\/span>('a[class=\"<\/span>sister\"]'<\/span><\/span><\/span>)
print soup.select<\/span>('a[href=\"<\/span>http:\/\/xxx.com\/elsie\"]')
# \u540c\u6837\uff0c\u5c5e\u6027\u4ecd\u7136\u53ef\u4ee5\u4e0e\u4e0a\u8ff0\u67e5\u627e\u65b9\u5f0f\u7ec4\u5408\uff0c\u4e0d\u5728\u540c\u4e00\u8282\u70b9\u7684\u7a7a\u683c\u9694\u5f00\uff0c\u540c\u4e00\u8282\u70b9\u7684\u4e0d\u52a0\u7a7a\u683c
<\/span> print soup.select<\/span>('p a[href=\"<\/span>http:\/\/xxx.com\/elsie\"]')
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0a\u7684 select \u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u679c\u90fd\u662f\u5217\u8868\u5f62\u5f0f\uff0c\u53ef\u4ee5\u904d\u5386\u5f62\u5f0f\u8f93\u51fa\uff0c\u7136\u540e\u7528 get_text() \u65b9\u6cd5\u6765\u83b7\u53d6\u5b83\u7684\u5185\u5bb9\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-21<\/span>
soup = BeautifulSoup(html, 'lxml'<\/span>)
print type(soup.select<\/span>('title'<\/span><\/span><\/span>))
print soup.select<\/span>('title')[0].get_text<\/span>()
 
for <\/span>title in <\/span>soup.select<\/span>('title'):
    print title.get_text<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u722c\u866bBeautiful Soupzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145],"tags":[],"class_list":["post-1557","post","type-post","status-publish","format-standard","hentry","category-pythonpcjc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1557"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1557"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1557\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}