chatbot, chat, robot

Line Notify + 網路爬蟲,機器人推送必須要知道

Python辛辛苦苦爬蟲下來的內容,經過整理過後,可以透過Line的通知進行發送推播嗎?必須來試試看~~

  • 1.掃描QRcode加入Line Notify好友

  • 2.在Line介面創立群組並邀請LINE Notify進群

    1. LINE Notify點此連結,登錄帳號密碼,選擇登錄服務
    1. 除了電子信箱一定要正確(要收認證信),其餘可以自行亂填
  • 5.信箱認證過後會出現

  • 6.點選管理登錄服務

  • 7.點選發行權杖

  • 8.自訂權杖名稱,並邀請進入群組

  • 9.請複製權杖,並將權杖記錄於筆記本妥善保管

  • 10.將權杖貼至程式碼內測試

    import requests
    
    token = ''
    
    requests.post(
        url='https://notify-api.line.me/api/notify',
        headers={
            'Authorization': f'Bearer {token}'
        },
        data={
            'message': ''
        }
    )
  • 11.群組將會顯示提示訊息


    Pchome電商進階應用

使用前請先取得proxies.csv檔案,參考Proxy網路爬蟲應用

import csv
import random
import urllib.parse

import munch
import requests

def readProxies(filename):
    proxies = []
    with open(filename, 'r') as f:
        reader = csv.DictReader(f)
        for row in reader:
            proxies.append(munch.munchify({
                'ip': row['IP'],
                'port': row['PORT']
            }))
    return proxies

def getProxy(proxies):
    index = random.randrange(len(proxies))
    return proxies.pop(index)

def sendLineNotify(msg):
    token = '<token>' #輸入發行權杖
    requests.post(
        url='https://notify-api.line.me/api/notify',
        headers={
            'Authorization': f'Bearer {token}'
        },
        data={
            'message': msg
        }
    )

proxies = readProxies('proxies.csv')

query = '電暖爐' #輸入搜尋關鍵字
query = urllib.parse.quote(query)
print(query)

proxy = None
while True:
    print(f'now have {len(proxies)} proxies')
    if proxy is None:
        print(f'get new proxy')
        proxy = getProxy(proxies)
        print(proxy)
        print(f'now have {len(proxies)} proxies')
    try:
        response = requests.get(
            url=f'https://ecshweb.pchome.com.tw/search/v3.3/all/results?q={query}&page=1&sort=sale/dc',
            proxies={
                'https': f'https://{proxy.ip}:{proxy.port}'
            },
            timeout=3
        )
        if response.status_code != 200:
            print(f'response status is not 200 ({response.status_code})')
        for prod in response.json()['prods']:
            prod = munch.munchify(prod)
            print(f'{prod.Id}    {prod.name}    {prod.price}')
            if prod.price != prod.originPrice:   
                sendLineNotify(f'{prod.name}    {prod.price}\nhttps://24h.pchome.com.tw/prod/{prod.Id}')  
        break
    except Exception as e:
        print(e)
        proxy = None
        print('proxy is dead')

Similar Posts

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。