{"id":3340,"date":"2023-05-05T23:59:57","date_gmt":"2023-05-05T15:59:57","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=3340"},"modified":"2023-05-07T06:27:37","modified_gmt":"2023-05-06T22:27:37","slug":"%e5%8d%95%e8%af%8d%e8%af%8d%e4%b9%89%e3%80%81%e8%af%8d%e6%80%a7%e3%80%81%e4%be%8b%e5%8f%a5%e6%9f%a5%e8%af%a2python%e4%bb%a3%e7%a0%81-%e8%be%93%e5%87%ba%e7%bb%93%e6%9e%9c%e6%9b%b4%e7%ae%80%e6%b4%81","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3340","title":{"rendered":"\u5355\u8bcd\u8bcd\u4e49\u3001\u8bcd\u6027\u3001\u4f8b\u53e5\u67e5\u8be2python\u4ee3\u7801-\u8f93\u51fa\u7ed3\u679c\u66f4\u7b80\u6d01"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#\u5355\u8bcd\u67e5\u8be2\u3001\u8f93\u51fa\u7ed3\u679c\u66f4\u7b80\u6d01\uff0c\u4e00\u6b21\u53ef\u67e5\u591a\u4e2a\u5355\u8bcd\u3001\u7a0b\u5e8f\u8f93\u51fa\u7684\u4f8b\u53e5\u6bd4\u8f83\u591a\u3002\n#\u67e5\u8be2\u5230\u7684\u5355\u8bcd\u5199\u5165\u5230words.txt\u6587\u4ef6\u4e2d\nimport requests\nfrom bs4 import BeautifulSoup\ndef query(word):\n    # \u83b7\u53d6\u6709\u9053\u8bcd\u5178\u7f51\u9875\u7684HTML\u4ee3\u7801\n    url = 'http:\/\/dict.youdao.com\/w\/%s' % word\n    html = requests.get(url).text\n\n    # \u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u4ee3\u7801\uff0c\u5e76\u83b7\u53d6\u5355\u8bcd\u8bfb\u97f3\u3001\u8bcd\u4e49\u548c\u8bcd\u6027\u53ca\u4f8b\u53e5\n    soup = BeautifulSoup(html, 'html.parser')\n    phonetic = soup.find(class_='phonetic').get_text().strip()   # \u83b7\u53d6\u5355\u8bcd\u7684\u8bfb\u97f3\n    trans_container = soup.find(id='phrsListTab')   # \u83b7\u53d6\u8bcd\u4e49\u548c\u4f8b\u53e5\u7684\u5bb9\u5668\n    trans = trans_container.find(class_='trans-container').get_text().strip() # \u83b7\u53d6\u8bcd\u4e49\n    examples = soup.find_all(class_='examples')  # \u83b7\u53d6\u4f8b\u53e5\n\n    # \u8f93\u51fa\u7ed3\u679c\n    with open('words.txt', 'a+', encoding=\"UTF-8\") as f:\n        f.write(word+\"\\n\")\n        f.write(phonetic+\"\\n\")\n        f.write(trans+\"\\n\")\n        for example in examples:\n            f.write(example.get_text().strip()+\"\\n\")\n\n    print('%s %s' % (word, phonetic))\n    print(trans)\n    print(\"-\" * 30)\n    print(\"\u4f8b\u53e5\uff1a\")\n    for example in examples:\n        print(example.get_text().strip())\n\n #\u8f93\u5165\u8981\u67e5\u8be2\u7684\u82f1\u8bed\u5355\u8bcd\nInput_Word = input(\"\u8bf7\u8f93\u5165\u8981\u67e5\u8be2\u7684\u5355\u8bcd\uff08\u5355\u8bcd\u7528'\/'\u5206\u5f00\uff09\uff1a\")\nlist_word = Input_Word.split('\/')\nfor lst in list_word:\n    query(lst)\n\n<\/code><\/pre>\n\n\n\n<p>\u7531cursor ai ide \u7f16\u8f91\u5668\u751f\u6210\u7684\u5355\u8bcd\u67e5\u8be2\u4ee3\u7801\uff0c\u53ea\u8f93\u51fa\u4e00\u4e2a\u4f8b\u53e5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># and extracts the text of the first \"p\" element within it.\n# Here is the modified code to get the phonetic transcription, acceptation, part of speech, and example sentence of a word:\n# # Import necessary libraries\nimport requests\nfrom bs4 import BeautifulSoup\n\n# Define the word to search for\nword = \"noise\"\n\n# Send a GET request to Youdao.com with the word as a parameter\nresponse = requests.get(f\"https:\/\/www.youdao.com\/w\/{word}\/#keyfrom=dict2.top\")\n\n# Parse the HTML content of the response using BeautifulSoup\nsoup = BeautifulSoup(response.content, \"html.parser\")\n\n# Find the phonetic transcription of the word\nphonetic = soup.find(\"div\", {\"class\": \"baav\"}).find(\"span\", {\"class\": \"pronounce\"}).text\n\n# Find the acceptation and part of speech of the word\nacceptation = soup.find(\"div\", {\"class\": \"trans-container\"}).find(\"ul\").find(\"li\").text\n\n# Find an example sentence using the word\nexample = soup.find(\"div\", {\"class\": \"examples\"}).find(\"p\").text\n\n# Print the phonetic transcription, acceptation, part of speech, and example sentence\nprint(f\"Phonetic transcription: {phonetic}\")\nprint(f\"Acceptation and part of speech: {acceptation}\")\nprint(f\"Example sentence: {example}\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7531cursor ai ide \u7f16\u8f91\u5668\u751f\u6210\u7684\u5355\u8bcd\u67e5\u8be2\u4ee3\u7801\uff0c\u53ea\u8f93\u51fa\u4e00\u4e2a\u4f8b\u53e5<\/p>\n","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-3340","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\/3340","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=3340"}],"version-history":[{"count":3,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3340\/revisions"}],"predecessor-version":[{"id":3347,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3340\/revisions\/3347"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3340"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}