{"id":3330,"date":"2023-05-04T19:56:48","date_gmt":"2023-05-04T11:56:48","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=3330"},"modified":"2023-05-04T19:56:50","modified_gmt":"2023-05-04T11:56:50","slug":"%e6%9c%89%e9%81%93%e5%ad%97%e5%85%b8%e4%b8%80%e6%ac%a1%e6%9f%a5%e5%a4%9a%e4%b8%aa%e5%8d%95%e8%af%8dpython%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/0.ipyingshe.net:5347\/?p=3330","title":{"rendered":"\u6709\u9053\u5b57\u5178\u4e00\u6b21\u67e5\u591a\u4e2a\u5355\u8bcdpython\u4ee3\u7801"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># \u5bfc\u5165requests\u548cBeautifulSoup\u5e93\r\nimport requests\r\nfrom bs4 import BeautifulSoup\r\n\r\n# \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u5355\u8bcd\u67e5\u8be2\u6709\u9053\u8bcd\u5178\uff0c\u5e76\u8fd4\u56de\u8bfb\u97f3\u3001\u8bcd\u4e49\u8bcd\u6027\u548c\u8bcd\u6027\u53d8\u5316\u53ca\u4f8b\u53e5\r\ndef query_youdao(word):\r\n    # \u6784\u9020\u6709\u9053\u8bcd\u5178\u7684\u67e5\u8be2\u7f51\u5740\r\n    url = \"http:\/\/dict.youdao.com\/w\/\" + word\r\n    # \u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\r\n    response = requests.get(url)\r\n    # \u89e3\u6790\u7f51\u9875\u5185\u5bb9\uff0c\u4f7f\u7528html.parser\u4f5c\u4e3a\u89e3\u6790\u5668\r\n    soup = BeautifulSoup(response.text, \"html.parser\")\r\n    # \u67e5\u627e\u8bfb\u97f3\u6240\u5728\u7684\u6807\u7b7e\uff0c\u4f7f\u7528find\u65b9\u6cd5\uff0c\u6839\u636eclass\u5c5e\u6027\u7b5b\u9009\r\n    phonetic = soup.find(class_=\"phonetic\")\r\n    # \u5982\u679c\u627e\u5230\u4e86\u8bfb\u97f3\u6807\u7b7e\uff0c\u5c31\u63d0\u53d6\u5176\u4e2d\u7684\u6587\u672c\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\r\n    if phonetic:\r\n        phonetic = phonetic.text\r\n    else:\r\n        phonetic = \"\"\r\n    # \u67e5\u627e\u8bcd\u4e49\u8bcd\u6027\u6240\u5728\u7684\u6807\u7b7e\uff0c\u4f7f\u7528find_all\u65b9\u6cd5\uff0c\u6839\u636eli\u6807\u7b7e\u7b5b\u9009\r\n    meanings = soup.find_all(\"li\")\r\n    # \u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u8bcd\u4e49\u8bcd\u6027\r\n    meaning_list = &#91;]\r\n    # \u904d\u5386\u6bcf\u4e2ali\u6807\u7b7e\uff0c\u63d0\u53d6\u5176\u4e2d\u7684\u6587\u672c\uff0c\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\r\n    for meaning in meanings:\r\n        meaning_list.append(meaning.text)\r\n    # \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7528\u6362\u884c\u7b26\u5206\u9694\u6bcf\u4e2a\u5143\u7d20\r\n    meaning_str = \"\\n\".join(meaning_list)\r\n    # \u67e5\u627e\u8bcd\u6027\u53d8\u5316\u6240\u5728\u7684\u6807\u7b7e\uff0c\u4f7f\u7528find\u65b9\u6cd5\uff0c\u6839\u636eclass\u5c5e\u6027\u7b5b\u9009\r\n    variations = soup.find(class_=\"additional\")\r\n    # \u5982\u679c\u627e\u5230\u4e86\u8bcd\u6027\u53d8\u5316\u6807\u7b7e\uff0c\u5c31\u63d0\u53d6\u5176\u4e2d\u7684\u6587\u672c\uff0c\u5e76\u53bb\u6389\u591a\u4f59\u7684\u7a7a\u683c\u548c\u6362\u884c\u7b26\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\r\n    if variations:\r\n        variations = variations.text.strip().replace(\"\\n\", \" \")\r\n    else:\r\n        variations = \"\"\r\n    # \u67e5\u627e\u4f8b\u53e5\u6240\u5728\u7684\u6807\u7b7e\uff0c\u4f7f\u7528find\u65b9\u6cd5\uff0c\u6839\u636eid\u5c5e\u6027\u7b5b\u9009\r\n    sentences = soup.find(id=\"bilingual\")\r\n    # \u5982\u679c\u627e\u5230\u4e86\u4f8b\u53e5\u6807\u7b7e\uff0c\u5c31\u63d0\u53d6\u5176\u4e2d\u7684\u6587\u672c\uff0c\u5e76\u53bb\u6389\u591a\u4f59\u7684\u7a7a\u683c\u548c\u6362\u884c\u7b26\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\r\n    if sentences:\r\n        sentences = sentences.text.strip().replace(\"\\n\", \" \")\r\n    else:\r\n        sentences = \"\"\r\n    # \u8fd4\u56de\u8bfb\u97f3\u3001\u8bcd\u4e49\u8bcd\u6027\u3001\u8bcd\u6027\u53d8\u5316\u548c\u4f8b\u53e5\u7ec4\u6210\u7684\u5143\u7ec4\r\n    return (phonetic, meaning_str, variations, sentences)\r\n\r\n# \u6d4b\u8bd5\u51fd\u6570\uff0c\u67e5\u8be2\u5355\u8bcd\"python\"\r\nInput_Word = input(\"\u8bf7\u8f93\u5165\u8981\u67e5\u8be2\u7684\u5355\u8bcd\uff1a\uff08\u5355\u8bcd\u7528'\/'\u5206\u5f00\uff09\")\r\nlist_word = Input_Word.split('\/')\r\nfor lst in list_word:\r\n        result = query_youdao(lst)\r\n        # \u6253\u5370\u7ed3\u679c\r\n        with open('words.txt','a+',encoding=\"UTF-8\") as f:\r\n            for word in result:\r\n                f.write(lst)\r\n                f.write(word)\r\n                f.write('\\n\\n')\r\n        print(result)\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":[10],"tags":[],"class_list":["post-3330","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3330"}],"version-history":[{"count":1,"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3330\/revisions"}],"predecessor-version":[{"id":3331,"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3330\/revisions\/3331"}],"wp:attachment":[{"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3330"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/0.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}