{"id":319,"date":"2023-10-05T14:58:05","date_gmt":"2023-10-05T06:58:05","guid":{"rendered":"https:\/\/lab.libra.tw\/?p=319"},"modified":"2023-10-05T14:58:05","modified_gmt":"2023-10-05T06:58:05","slug":"python%e7%88%ac%e8%9f%b2-x-line-notify","status":"publish","type":"post","link":"https:\/\/lab.libra.tw\/?p=319","title":{"rendered":"python\u722c\u87f2 X line notify"},"content":{"rendered":"\n<p>\u7db2\u8def\u722c\u87f2\u662f\u4e00\u7a2e\u7a0b\u5f0f\uff0c\u6a21\u64ec\u4e00\u822c\u4eba\u700f\u89bd\u7db2\u8def\u7684\u884c\u70ba\uff0c\u4e26\u85c9\u7531\u7a0b\u5f0f\u641c\u96c6\u6211\u5011\u6240\u9700\u8981\u7684\u8cc7\u8a0a\uff0c\u6216\u8005\u9032\u884c\u8a02\u7968\u4e4b\u985e\u7684\u884c\u70ba\u3002\u521d\u5b78python\u7a0b\u5f0f\u53ca\u722c\u87f2\u7684\u6211\uff0c\u60f3\u7528\u722c\u87f2\u4f86\u5354\u52a9\u6211\u8ffd\u8e64PTT MacShop\u7248\u7684\u6587\u7ae0\uff0c\u7576\u6211\u60f3\u8cfc\u8cb7\u7684\u5546\u54c1\uff0c\u51fa\u73fe\u5728\u6587\u7ae0\u6a19\u984c\u6642\uff0c\u4ee5line notify\u901a\u77e5\u6211\uff0c\u4e26\u63d0\u4f9b\u9023\u7d50\uff0c\u9019\u76ee\u7684\u7576\u7136\u662f\u60f3\u6436\u5546\u54c1\uff0c\u6211\u4e26\u4e0d\u80fd\u7121\u6642\u7121\u523b\u76ef\u8457\u7248\u9762\uff0c\u901a\u5e38\u770b\u5230\u6211\u60f3\u8cb7\u7684\u5546\u54c1\u6642\uff0c\u5df2\u7d93\u88ab\u4eba\u8cb7\u8d70\uff0c\u5982\u679c\u6709\u722c\u87f2\u53ef\u4ee5\u5e6b\u5fd9\u5b9a\u6642reveiw\u4e00\u4e0b\u7248\u9762\u7684\u6a19\u984c\uff0c\u5c31\u53ef\u4ee5\u589e\u52a0\u6211\u8cb7\u5230\u7684\u6a5f\u7387\u4e86\u3002<\/p>\n\n\n\n<p>\u4ee5python\u9032\u884c\u7db2\u8def\u722c\u87f2\uff0c\u9700\u8981\u5169\u500b\u5957\u4ef6\uff0crequests\u4ee5\u53cabeautiful soup<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>requests &#8211; \u7528\u65bc\u9001\u51fa\u8acb\u6c42\u53ca\u53d6\u5f97\u4f3a\u5668\u6240\u56de\u50b3\u7684\u8cc7\u8a0a\u8cc7\u6599<\/li>\n\n\n\n<li>beautiful soup &#8211; \u7528\u65bc\u89e3\u6790HTML\u3001XML\u6587\u4ef6\uff0c\u4ee5\u65b9\u4fbf\u53d6\u51fa\u6587\u4ef6\u7684\u5167\u5bb9<\/li>\n<\/ul>\n\n\n\n<p>\u53e6\u5916\u6703\u7528\u5230lxml\u9019\u500b\u89e3\u6790\u5668\uff0cbeautiful soup\u5167\u5efa\u7684\u662fhtml.parser\uff0c\u6240\u4ee5\u9700\u8981\u53e6\u5916\u5167\u88dd\u9019\u500b\u89e3\u6790\u5668<\/p>\n\n\n\n<p>line notify\u662fline\u63d0\u4f9b\u7684\u7684API\uff0c\u7528\u65bc\u63a8\u64ad\u8a0a\u606f\uff0c\u7531\u65bc\u662f\u514d\u8cbb\uff0c\u6240\u4ee5\u6709\u4e9b\u9650\u5236(\u5fc5\u9808\u4e3b\u52d5\u8a02\u95b1\u3001\u6bcf\u5206\u9418\u63a8\u64ad\u6578\u91cf\u9650\u5236)\uff0c\u4f46\u662f\u4e00\u822c\u81ea\u5df1\u4f7f\u7528\u4e5f\u5920\u4e86\uff0c\u91cd\u9ede\u662f\u514d\u8cbb\u3002\u56e0\u70baline notify\u7684<a href=\"https:\/\/www.google.com\/search?q=line+notify%E6%95%99%E5%AD%B8&amp;client=firefox-b-d&amp;sca_esv=568527130&amp;sxsrf=AM9HkKlOxguqROZqwJOgkx_OiS-bpAV11A%3A1695740780794&amp;ei=bPMSZY2VMMvCoATN3pLYDw&amp;ved=0ahUKEwiN-_rWxsiBAxVLIYgKHU2vBPsQ4dUDCA8&amp;uact=5&amp;oq=line+notify%E6%95%99%E5%AD%B8&amp;gs_lp=Egxnd3Mtd2l6LXNlcnAiEWxpbmUgbm90aWZ55pWZ5a24MgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADMgoQABhHGNYEGLADSNkzUKMLWNgvcAd4AZABAJgBTKAB1QKqAQE2uAEDyAEA-AEBwgIHECMYsAIYJ8ICBxAAGA0YgATCAgcQIRigARgKwgIIEAAYCBgeGA3CAgYQABgeGA3iAwQYACBBiAYBkAYK&amp;sclient=gws-wiz-serp\">\u6559\u5b78<\/a>\u5df2\u7d93\u76f8\u7576\u591a\uff0c\u9019\u908a\u53ea\u63d0\u4f9b\u7a0b\u5f0f\u78bc\uff0c\u6b0a\u6756(token)\u7684\u90e8\u4efd\u8acb\u81ea\u884c\u5728line notify\u5b98\u65b9\u7db2\u7ad9\u7533\u8acb\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7a0b\u5f0f\u78bc<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#import\u6240\u9700\u5957\u4ef6\nimport requests\nfrom bs4 import BeautifulSoup\n\n\n#==========line notify function==================\ndef linenotify( msg ):\n    url = 'https:\/\/notify-api.line.me\/api\/notify'\n    token = 'token\u8cbc\u5728\u9019 \u55ae\u5f15\u865f\u5167'\n    headers = {\n        'Authorization': 'Bearer ' + token                # \u8a2d\u5b9a LINE Notify \u6b0a\u6756\n    }\n    data = { \n    'message':(\"{}\".format( msg ))                        # \u8981\u63a8\u64ad\u51fa\u53bb\u7684\u8a0a\u606f \uff0c \"message:\" ( \u4e0d\u53ef\u5c11 )\n    }\n    #image = open('\/path', 'rb')                          # \u8a2d\u5b9a\u5716\u7247\u8def\u5f91\uff0c\u4ee5\u4e8c\u9032\u4f4d\u65b9\u5f0f\u958b\u555f\u5716\u7247\n    #imageFile = {'imageFile' : image}                    # \u8a2d\u5b9a\u5716\u7247\u6a94\u540d\n    #data = requests.post(url, headers=headers, data=data, files=imageFile)   # \u767c\u9001 LINE Notify(\u8a0a\u606f+\u5716\u7247)\n    data = requests.post(url, headers=headers, data=data)                     # \u767c\u9001 LINE Notify(\u53ea\u6709\u8a0a\u606f)\n#==========================================\n'''\n#\u90e8\u4efd\u7db2\u7ad9\u6703\u6a94\u722c\u87f2\uff0c\u6709\u9700\u8981\u53ef\u5c07headers\u52a0\u5165\nheaders = {\n        \"Content-Type\": \"application\/json;charset=UTF-8\",\n        \"User-Agent\": \"Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko)\"\n    }\n'''\nptt_url = \"https:\/\/www.ptt.cc\/bbs\/MacShop\/index.html\"             # ptt macshop\u7248\u7db2\u5740\nr = requests.get(' ptt_url ' )                                    # requests\u767c\u51faget\u8acb\u6c42\uff0c\u5c07\u56de\u50b3\u539f\u59cb\u8cc7\u6599\u5b58\u5165r\nsp = BeautifulSoup( r.text,  'lxml' )                             # beautifulsoup\u89e3\u6790\u56de\u50b3\u8cc7\u6599\uff0c\u5b58\u5165sp\n\n'''\n\u81ea\u8eab\u5148\u5c0d\u539f\u59cb\u78bcreview\u904e\uff0c\u5f97\u77e5\u6240\u7684\u8cc7\u6599\u7684\u6a19\u7c64(tag)\u4f4d\u7f6e\uff0c\n\u518d\u4ee5 .find_all \u6216 .find \u7684\u65b9\u6cd5\u5c07\u8cc7\u6599\u53d6\u51fa\n\u4f7f\u7528\u8005\u9700\u8981\u5c0dHTML\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\n''''\ntitle = sp.find_all( 'div', class_ = 'title')               # \u63f4\u5c0b\u6240\u6709div tag\uff0cclass\u662ftitle\uff0c\n                                                              class\u662fpython\u4fdd\u7559\u5b57\uff0c\u6240\u4ee5\u9700\u8981\u52a0 _\n\nlinks = &#91;]                                                  # \u7528\u65bc\u5132\u4f9f\u6587\u7ae0url\u7684list\ntitles =&#91;]                                                  # \u7528\u65bc\u5132\u5b58\u6587\u7ae0\u6a19\u984c\u7684list\nfor div in title:                                           # \u8ff4\u5708\u5224\u65b7\u6a19\u984c\u662f\u5426\u6709url\uff0c\u53ca\u6a19\u984c\u662f\u5426\u6709\u95dc\u9375\u5b57\n    a = div.find('a', href=True)\n    if a is not None:                                       # \u6709url\uff0c\u518d\u5224\u65b7\u6a19\u984c\u662f\u5426\u6709\u95dc\u9375\u5b57\n       if div.text.find( 'HomePod' ) != -1 or div.text.find( 'Homepod' ) != -1 or div.text.find( 'homepod' ) != -1\\\n               and div.text.find('mini') != -1:\n          if div.text.find('\u8ca9\u552e') != -1:                    # \u518d\u5224\u65b7\u6a19\u984c\u662f\u5426\u70ba\u8ca9\u552e\n            titles.append(div.text.strip())                        # \u6a19\u984c\u5b58\u5165\u4e32\u5217\n            links.append((\"{}{}\").format( ptt_url,a&#91;'href'] ))     # url\u5b58\u5165\u4e32\u5217\n\nline_msg = zip( titles, links )                                    # \u5c07\u6a19\u984c\u3001url\u7d44\u6210\u4e8c\u7dad\u4e32\u5217\n\nsendmsg = \"\\n\"                                                     # \u751f\u6210line notify\u6240\u9700\u8981\u7684\u5b57\u4e32(\u8a0a\u606f) sendmsg\nfor l in line_msg:\n    sendmsg = sendmsg + l&#91;0]+\"\\n\"+l&#91;1]+\"\\n\"                        # \u8a0a\u606f\u7d44\u6210\"\u6a19\u984c+\u63db\u884c+url+\u63db\u884c\"\n\n\nif len( sendmsg ) &gt; 1:                                             # \u5224\u65b7\u6709\u8a0a\u606f\u624d\u63a8\u64ad\n    linenotify( sendmsg )<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u7a0b\u5f0f\u57f7\u884c\u4e00\u6b21\uff0c\u6703\u91dd\u5c0d\u7db2\u9801\u7248MacShop\u7248\u7b2c\u4e00\u9801\u722c\u6587\u4e00\u6b21\uff0c\u5c07\u7b26\u5408\u8981\u6c42\u7684\u6a19\u984c\u5305\u542b\u7db2\u5740\u53d6\u51fa\uff0c\u518d\u4ee5line notify\u767c\u51fa\uff0c\u4f46\u662f\u5982\u679c\u5f97\u81ea\u5df1\u57f7\u884c\uff0c\u4e0d\u5982\u81ea\u884c\u958b\u555f\u7db2\u9801\u770b\u4e00\u904d\uff0c\u6240\u4ee5\u5fc5\u9808\u8981\u8b93\u9019\u652f\u7a0b\u5f0f\u5b9a\u6642\u57f7\u884c\u3002<\/p>\n\n\n\n<p>\u56e0\u70ba\u81ea\u8eab\u6709\u67b6server\uff0c24\u5c0f\u6642\u4e0d\u95dc\u6a5f\uff0c\u6240\u4ee5\u5c31\u5c07\u7a0b\u5f0f\u5beb\u7a0b\u5f0f\u6539\u6210python script\uff0c\u5229\u7528linux\u7684crontab\u9032\u884c\u6392\u7a0b\uff0c\u6bcf30\u5206\u9418\u57f7\u884c\u4e00\u6b21\uff0cLine\u5c31\u6703\u50cf\u4e0b\u5716\u6536\u5230\u901a\u77e5\uff0c\u5982\u679c\u9996\u9801\u88e1\u6c92\u6709\u7b26\u5408\u689d\u4ef6\u7684\u6a19\u984c\uff0c\u5c31\u4e0d\u6703\u6536\u5230\u8a0a\u606f\uff0c<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"1024\" src=\"https:\/\/lab.libra.tw\/wp-content\/uploads\/2023\/10\/IMG_5237-472x1024.png\" alt=\"\" class=\"wp-image-323\" srcset=\"https:\/\/lab.libra.tw\/wp-content\/uploads\/2023\/10\/IMG_5237-472x1024.png 472w, https:\/\/lab.libra.tw\/wp-content\/uploads\/2023\/10\/IMG_5237-138x300.png 138w, https:\/\/lab.libra.tw\/wp-content\/uploads\/2023\/10\/IMG_5237-768x1665.png 768w, https:\/\/lab.libra.tw\/wp-content\/uploads\/2023\/10\/IMG_5237-709x1536.png 709w, https:\/\/lab.libra.tw\/wp-content\/uploads\/2023\/10\/IMG_5237-945x2048.png 945w, https:\/\/lab.libra.tw\/wp-content\/uploads\/2023\/10\/IMG_5237.png 1290w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7db2\u8def\u722c\u87f2\u662f\u4e00\u7a2e\u7a0b\u5f0f\uff0c\u6a21\u64ec\u4e00\u822c\u4eba\u700f\u89bd\u7db2\u8def\u7684\u884c\u70ba\uff0c\u4e26\u85c9\u7531\u7a0b\u5f0f\u641c\u96c6\u6211\u5011\u6240\u9700\u8981\u7684\u8cc7\u8a0a\uff0c\u6216\u8005\u9032\u884c\u8a02\u7968\u4e4b\u985e\u7684\u884c\u70ba &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[27],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-9","tag-python"],"_links":{"self":[{"href":"https:\/\/lab.libra.tw\/index.php?rest_route=\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lab.libra.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lab.libra.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lab.libra.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lab.libra.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319"}],"version-history":[{"count":3,"href":"https:\/\/lab.libra.tw\/index.php?rest_route=\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/lab.libra.tw\/index.php?rest_route=\/wp\/v2\/posts\/319\/revisions\/324"}],"wp:attachment":[{"href":"https:\/\/lab.libra.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lab.libra.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lab.libra.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}