{"id":7309,"date":"2024-04-08T10:01:01","date_gmt":"2024-04-08T02:01:01","guid":{"rendered":""},"modified":"2024-04-08T10:01:01","modified_gmt":"2024-04-08T02:01:01","slug":"python\u4fe1\u606f\u6807\u8bb0","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7309.html","title":{"rendered":"python\u4fe1\u606f\u6807\u8bb0"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4e09\u79cd\u6807\u8bb0\u5f62\u5f0f
xml
json
yaml<\/p>\n

>>> for link in soup.find_all('a')\nSyntaxError: invalid syntax\n>>> for link in soup.find_all('a'):#\u67e5\u627e\u6240\u6709\u6807\u7b7ea\n\tprint(link.get('href'))\n\n\t\nhttp:\/\/www.icourse163.org\/course\/BIT-268001\nhttp:\/\/www.icourse163.org\/course\/BIT-1001870001\n<\/code><\/pre>\n

\u5185\u5bb9\u67e5\u627e\u65b9\u6cd5<\/p>\n

Help on method find_all in module bs4.element:\n\nfind_all(name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs) method of bs4.BeautifulSoup instance\n    Extracts a list of Tag objects that match the given\n    criteria.  You can specify the name of the Tag and any\n    attributes you want the Tag to have.\n    \n    The value of a key-value pair in the 'attrs' map can be a\n    string, a list of strings, a regular expression object, or a\n    callable that takes a string and returns whether or not the\n    string matches for some custom definition of 'matches'. The\n    same is true of the tag name.\n<\/code><\/pre>\n

name:\u5bf9\u6807\u7b7e\u540d\u79f0\u7684\u68c0\u7d22\u5b57\u7b26\u4e32\u3002
attrs:\u5bf9\u6807\u7b7e\u5c5e\u6027\u503c\u7684\u68c0\u7d22\u5b57\u7b26\u4e32\uff0c\u53ef\u6807\u6ce8\u5c5e\u6027\u68c0\u7d22\u3002
recursive:\u662f\u5426\u5bf9\u5b50\u5b59\u5168\u90e8\u68c0\u7d22\uff0c\u9ed8\u8ba4True
text:<>\u2026<\/>\u4e2d\u5b57\u7b26\u4e32\u533a\u57df\u7684\u68c0\u7d22\u5b57\u7b26\u4e32\u3002<\/p>\n

>>> for tag in soup.find_all(True):#\u67e5\u627e\u6240\u6709\u6807\u7b7e\n\tprint(tag.name)\n\n\t\nhtml\nhead\ntitle\nbody\np\nb\np\na\na\n<\/code><\/pre>\n
>>> import re\n>>> for tag in soup.find_all(re.compile('b')):#\u67e5\u627e\u6240\u6709\u4ee5b\u5f00\u5934\u7684\u6807\u7b7e\n\tprint(tag.name)\n\n\t\nbody\nb\n<\/code><\/pre>\n
>>> soup.find_all('p','course')#p\u6807\u7b7e\u7684course\u5c5e\u6027\n>>> soup.find_all(id='link1')\n[<a class=\"py1\" href=\"http:\/\/www.icourse163.org\/course\/BIT-268001\" id=\"link1\">Basic Python<\/a>]\n>>> soup.find_all(id='link')\n[]\n>>> soup1.find_all(id='href')\n[]\n>>> soup.find_all(id=re.compile('link'))\n[<a class=\"py1\" href=\"http:\/\/www.icourse163.org\/course\/BIT-268001\" id=\"link1\">Basic Python<\/a>, <a class=\"py2\" href=\"http:\/\/www.icourse163.org\/course\/BIT-1001870001\" id=\"link2\">Advanced Python<\/a>]\n>>> soup1.find_all(re.compile('page'))\n[]\n<\/code><\/pre>\n

\u6269\u5c55\u65b9\u6cd5<\/p>\n\n\n\n\n\n\n\n\n\n\n\n
\u65b9\u6cd5<\/th>\n\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n
<>.find()<\/td>\n\u641c\u7d22\u4e14\u53ea\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u540c.find_all()\u53c2\u6570<\/td>\n<\/tr>\n
<>.find_parents()<\/td>\n\u5728\u5148\u8f88\u8282\u70b9\u4e2d\u641c\u7d22\uff0c\u8fd4\u56de\u5217\u8868\u7c7b\u578b\uff0c\u540c.find_all()\u53c2\u6570<\/td>\n<\/tr>\n
<>.find_parent()<\/td>\n\u5728\u5148\u8f88\u8282\u70b9\u4e2d\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u540c.find()\u53c2\u6570<\/td>\n<\/tr>\n
<>.find_next_siblings()<\/td>\n\u5728\u540e\u7eed\u5e73\u884c\u8282\u70b9\u4e2d\u641c\u7d22\uff0c\u8fd4\u56de\u5217\u8868\u7c7b\u578b\uff0c\u540c.find_all()\u53c2\u6570<\/td>\n<\/tr>\n
<>.find_next_sibling()<\/td>\n\u5728\u540e\u7eed\u5e73\u884c\u8282\u70b9\u4e2d\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u540c.find()\u53c2\u6570<\/td>\n<\/tr>\n
<>.find_previous_siblings()<\/td>\n\u5728\u524d\u5e8f\u5e73\u884c\u8282\u70b9\u4e2d\u641c\u7d22\uff0c\u8fd4\u56de\u5217\u8868\u7c7b\u578b\uff0c\u540c.find_all()\u53c2\u6570<\/td>\n<\/tr>\n
<>.find_previous_sibling()<\/td>\n\u5728\u524d\u5e8f\u5e73\u884c\u8282\u70b9\u4e2d\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\uff0c\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u540c.find()\u53c2\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
>>> soup1.find_all(id=re.compile('next'))\n[<a href=\"\/page\/2\" id=\"next\">\u4e0b\u4e00\u9801<\/a>]\n>>> soup1.find_all(string='\u9ad8\u6e05')\n['\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05']\n>>> soup.find_all('a',recursive=False)\n[]\n>>> soup.find_all('a',recursive=True)\n[<a class=\"py1\" href=\"http:\/\/www.icourse163.org\/course\/BIT-268001\" id=\"link1\">Basic Python<\/a>, <a class=\"py2\" href=\"http:\/\/www.icourse163.org\/course\/BIT-1001870001\" id=\"link2\">Advanced Python<\/a>]\n>>> soup1.find_all(text='\u9ad8\u6e05')\n['\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05']\n>>> soup1.find_all(text=re.compile('\u6e05'))\n['\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u6e05\u695a'\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05', '\u9ad8\u6e05']\n>>> for link in soup1.find_all('a',text='\u9ad8\u6e05')\uff1a\nSyntaxError: invalid character in identifier\n>>> for link in soup1.find_all('a',text='\u9ad8\u6e05'):\n\tprint(link.get('href'))\n\n\n>>> \n>>> soup1.find_all('a')\n\n>>> soup1.find_all('a',string='\u9ad8\u6e05')\n[<a href=\"https:\/\/www.xxx\/hd\">\u9ad8\u6e05<\/a>]\n>>> soup1.find_all('a','button')\n[]\n>>> soup('a')\n[<a class=\"py1\" href=\"http:\/\/www.icourse163.org\/course\/BIT-268001\" id=\"link1\">Basic Python<\/a>, <a class=\"py2\" href=\"http:\/\/www.icourse163.org\/course\/BIT-1001870001\" id=\"link2\">Advanced Python<\/a>]\n>>> \n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"python\u4fe1\u606f\u6807\u8bb0\u4e09\u79cd\u6807\u8bb0\u5f62\u5f0fxmljsonyamlforlinkinsoup.find_all('a')SyntaxError:invalidsyntaxforlinkinsoup.find_all('a'):...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7309"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7309"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7309\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}