如何利用Python監(jiān)控別人的網(wǎng)站
場(chǎng)景
可能是你用不到,但是我遇到了這樣一個(gè)問題,就是我想詳細(xì)了解我的競(jìng)爭(zhēng)對(duì)手的網(wǎng)站(電商類)銷售情況和新品上架情況,但是我總不至于像盯盤一樣,在電腦或者手機(jī)上一直看著這個(gè)站吧!
于是我想到用一個(gè)腳本來檢測(cè),腳本的功能是如果發(fā)現(xiàn)對(duì)手出售了商品,就發(fā)送我售出商品的名稱,價(jià)格;如果是新上線了商品,就要郵件告訴我新品的名稱,價(jià)格,這對(duì)于我分析對(duì)手的銷量和趨勢(shì),然后在我的店鋪中擇優(yōu)上貨是有一定幫助作用的。
另外,這個(gè)腳本作用不僅僅如此,你也可以修改一下,包括但不限于監(jiān)控自己的抖音粉絲上升趨勢(shì)、其他事件新聞進(jìn)展、甚至是當(dāng)前熱點(diǎn)等等。
現(xiàn)成的產(chǎn)品
當(dāng)然,我前面的想法在現(xiàn)實(shí)中每個(gè)公司、店鋪都用得到,而且有人專門開發(fā)程序?yàn)榇硕?wù),比方說比較好的網(wǎng)頁監(jiān)控工具:Visualping、Distill Web Monitor、Wachete等,他們都是做這個(gè)的也都很專業(yè),但是要想深度使用,就要收費(fèi)了;
國內(nèi)也有類似的產(chǎn)品,我嘗試過。不過也有限制,每天1個(gè)網(wǎng)頁變化只給10封郵件,申請(qǐng)?jiān)俣嘁惨召M(fèi)!
所以吧,自己寫來自己用吧!
代碼
import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
# 網(wǎng)站 URL
url = "https://"
# 發(fā)送郵件的參數(shù)
sender = '你的發(fā)件人郵箱'
receiver = '你的收件人郵箱'
smtp_server = 'smtp.xxx.com' # 發(fā)件人郵箱的 SMTP 服務(wù)器地址
smtp_port = 465 # 發(fā)件人郵箱的 SMTP 端口
username = '你的發(fā)件人郵箱'
password = '你的發(fā)件人郵箱密碼'
def send_email(subject, body):
# 創(chuàng)建 MIMEText 郵件
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
# 發(fā)送郵件
with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
server.login(username, password)
server.sendmail(sender, receiver, msg.as_string())
def get_product_info(product_url):
# 獲取商品信息
response = requests.get(product_url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.find('h1', class_='h3').text
price = soup.find('span', class_='h2').text
stock = soup.find('span', class_='js-product-stock').text
return (title, price, stock)
def check_product_sold_out(product_url):
# 檢查商品是否已售出
response = requests.get(product_url)
soup = BeautifulSoup(response.text, 'html.parser')
if soup.find('div', class_='product-sold-out'):
return True
else:
return False
# 定期檢查商品
while True:
# 獲取網(wǎng)頁內(nèi)容
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 查找商品列表
product_list = soup.find('div', class_='js-product-list')
# 檢查每個(gè)商品是否售出
for product in product_list.find_all('a', class_='product-card'):
product_url = product['href']
product_title = product.find('h2').text
if check_product_sold_out(product_url):
# 商品已售出,發(fā)送郵件通知
subject = f'商品已售出:{product_title}'
body = f'商品名稱:{product_title}\n'
send_email(subject, body)
else:
# 商品未售出,檢查是否為新商品
try:
# 嘗試獲取商品信息,如果獲取失敗則說明是新商品
product_title, product_price, product_stock = get_product_info(product_url)
except:
# 新商品,發(fā)送郵件通知
subject = f'新商品上架:{product_title}'
body = f'商品名稱:{product_title}\n庫存:{product_stock}\n價(jià)格:{product_price}\n'
send_email(subject, body)
# 等待一段時(shí)間后再次檢查
time.sleep(300)
解釋:這個(gè)就是網(wǎng)店的監(jiān)控程序,新品上架,商品售出會(huì)像間諜一樣及時(shí)通知你!
再贈(zèng)送一個(gè):
import requests
import hashlib
import time
import smtplib
from email.mime.text import MIMEText
url = 'https://'
def get_hash(url):
response = requests.get(url)
return hashlib.sha256(response.content).hexdigest()
def send_email(content):
sender = ''
receiver = ''
password = ''
smtp_server = ''
smtp_port = 465
message = MIMEText(content)
message['From'] = sender
message['To'] = receiver
message['Subject'] = 'Website Change Alert'
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.login(sender, password)
server.sendmail(sender, receiver, message.as_string())
server.quit()
current_hash = get_hash(url)
while True:
new_hash = get_hash(url)
if new_hash != current_hash:
send_email('Website content has changed.')
current_hash = new_hash
else:
time.sleep(30)
解釋:這個(gè)代碼很簡(jiǎn)單,比較的是網(wǎng)頁哈希,只要有變化就會(huì)郵件通知,可用于任何場(chǎng)景!
如何使用
上面的代碼是python的,修改后可以直接使用。后臺(tái)運(yùn)行的方法:
nohup python3 jiankong.py > output.log 2>&1 &
到此這篇關(guān)于如何利用Python監(jiān)控別人的網(wǎng)站的文章就介紹到這了,更多相關(guān)Python監(jiān)控網(wǎng)站內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm實(shí)現(xiàn)在子類中添加一個(gè)父類沒有的屬性
這篇文章主要介紹了pycharm實(shí)現(xiàn)在子類中添加一個(gè)父類沒有的屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Django ValuesQuerySet轉(zhuǎn)json方式
這篇文章主要介紹了Django ValuesQuerySet轉(zhuǎn)json方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python sklearn庫實(shí)現(xiàn)簡(jiǎn)單邏輯回歸的實(shí)例代碼
Scikit-learn(sklearn)是機(jī)器學(xué)習(xí)中常用的第三方模塊,對(duì)常用的機(jī)器學(xué)習(xí)方法進(jìn)行了封裝,這篇文章主要介紹了python sklearn庫實(shí)現(xiàn)簡(jiǎn)單邏輯回歸的實(shí)例代碼,需要的朋友可以參考下2019-07-07
Python內(nèi)置函數(shù) next的具體使用方法
這篇文章主要介紹了Python內(nèi)置函數(shù) next的具體使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Python實(shí)現(xiàn)EM算法實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)EM算法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python數(shù)據(jù)處理利器Slice函數(shù)用法詳解
這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)處理利器Slice函數(shù)用法的相關(guān)資料,slice函數(shù)是Python中的一個(gè)內(nèi)置函數(shù),用于對(duì)序列進(jìn)行切片操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
Android 兼容性問題:java.lang.UnsupportedOperationException解決辦法
這篇文章主要介紹了Android 兼容性問題:java.lang.UnsupportedOperationException解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
python3 wechatpy微信支付的項(xiàng)目實(shí)踐
本文主要介紹了python3 wechatpy微信支付的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

