{"id":3409,"date":"2023-05-26T00:38:36","date_gmt":"2023-05-25T16:38:36","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3409"},"modified":"2023-05-26T00:38:38","modified_gmt":"2023-05-25T16:38:38","slug":"python-beautifulsoup4%e5%ba%93%e7%88%ac%e5%8f%96%e7%99%be%e5%ba%a6%e7%bd%91%e9%a6%96%e9%a1%b5%e4%b8%8a%e7%9a%84%e6%96%87%e5%ad%97%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3409","title":{"rendered":"python BeautifulSoup4\u5e93\u722c\u53d6\u767e\u5ea6\u7f51\u9996\u9875\u4e0a\u7684\u6587\u5b57\u5185\u5bb9"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#python bs4\u5e93\u83b7\u53d6\u767e\u5ea6\u7f51\u9996\u9875\u4e0a\u7684\u6587\u5b57\u5185\u5bb9\r\nimport requests\r\nfrom bs4 import BeautifulSoup\r\n\r\n#requests \u8bfb\u53d6\u7f51\u9875\u5185\u5bb9res.content\u6216\u6587\u672cres.text\r\nurl = \"https:\/\/www.baidu.com\"\r\nres = requests.get(url).content.decode(\"utf8\")\r\nprint(res)\r\n#print(res.content.decode(\"utf8\"))\r\n# res = requests.get(url)\r\n# print(res.text)\r\n\r\nsoup = BeautifulSoup(res,\"html5lib\")\r\n# result = soup.a.string   #\u53ef\u83b7\u53d6\u4e00\u4e2aa\u6807\u7b7e\u5185\u6587\u672c\r\n# print(result)\r\n\r\n#\u8bfb\u53d6a\uff0cclass=mnav\u6807\u7b7e\u6587\u672c\r\nprint(\"## \u8bfb\u53d6a\uff0cclass=mnav\u6807\u7b7e\u6587\u672c\")\r\nresults = soup.find_all('a', {'class': 'mnav'})\r\nfor result in results:\r\n    print(result.string)\r\n\r\n#\u8bfb\u53d6a\uff0cclass=mnav\u6807\u7b7e\u6587\u672c,\u653e\u5165name\u5217\u8868\u4e2d\r\nprint(\"## \u8bfb\u53d6a\uff0cclass=mnav\u6807\u7b7e\u6587\u672c,\u653e\u5165name\u5217\u8868\u4e2d\")\r\nname = &#91;]\r\nitems = soup.find_all(\"a\",class_=\"mnav\")\r\nfor item in items:\r\n    #print(item.string)\r\n    name.append(item.string)\r\nprint(name)\r\n\r\n#\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5html\u4e2d\u8bfb\u53d6\u6587\u672c  \r\nprint(\"## \u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5html\u4e2d\u8bfb\u53d6\u6587\u672c  \")\r\nimport re\r\nitems = soup.find_all(\"a\",class_=\"mnav\")\r\nfor item in items:\r\n    # print(type(item))\r\n    pattern = r'>(.*?)&lt;'\r\n    result = re.findall(pattern, str(item))\r\n    print(result)      \r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3409","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3409"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3409\/revisions"}],"predecessor-version":[{"id":3410,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3409\/revisions\/3410"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3409"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}