#mongodb #web-scraping #beautifulsoup #string-comparison
#mongodb #соскабливание полотна #beautifulsoup #сравнение строк
Вопрос:
Я люблю виски, но я не разработчик. И я пытаюсь написать скрипт для очистки данных с сайта, а затем уведомлять меня о новых продуктах на этом сайте. Но у меня проблемы, скрипт работает некорректно. Он не уведомляет меня и о пропущенных новых продуктах. Кто-нибудь может мне помочь, пожалуйста? Я использую cron для выполнения своего скрипта каждые 5 минут.
#!/usr/bin/python3
from bs4 import BeautifulSoup
import requests
import time
import pymongo
import difflib
import functools
import numpy as np
import telebot
URL = 'https://www.thewhiskyexchange.com/new-products/standard-whisky'
base = "https://www.thewhiskyexchange.com"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
client = pymongo.MongoClient('localhost:27017')
db = client.Scrape.WhiskeyExchange
content = requests.get(URL, headers=headers)
soup = BeautifulSoup(content.text, 'html.parser')
bot = telebot.TeleBot('MyApiKey')
bot_chatID2 = 'MychatID'
bot_chatID = 'MyGroupChatID'
def scrapeItems():
itemList = []
r = requests.get(URL, headers=headers)
soup = BeautifulSoup(r.content, 'lxml')
for listing in soup.select('.product-list-item'):
name = listing.select_one('.information p').text
link = base listing.select_one('.product-list-item a')['href']
price = listing.select_one('.price') # price
if not price is None: # not every listing has a price
price = price.text
else:
price = np.NaN
itemList.append(
{'name': name,
'link': link,
'price': price,
}
)
#print(itemList)
return itemList
def newScrapeItems():
newItemList = []
r = requests.get(URL, headers=headers)
soup = BeautifulSoup(r.content, 'lxml')
for listing in soup.select('.product-list-item'):
name = listing.select_one('.information p').text
link = base listing.select_one('.product-list-item a')['href']
price = listing.select_one('.price') # price
if not price is None: # not every listing has a price
price = price.text
else:
price = np.NaN
newItemList.append(
{'name': name,
'link': link,
'price': price,
}
)
# print(itemList)
return newItemList
items = scrapeItems()
time.sleep(350)
newItems = newScrapeItems()
links = 'n '.join([i["link"] for i in newItems])
def comparingProducts():
# compare two lists for difference
if functools.reduce(lambda x, y: x and y, map(lambda p, q: p == q, items, newItems), True):
print("Lists items and newItems has not difference between, exit...")
bot.send_message(bot_chatID, links)
else:
print("Lists items and newItems has difference between!, doing next steps")
list_difference = []
for item in newItems:
if item not in items:
newLinks = 'n '.join([i["link"] for i in items])
list_difference.append(item)
bot.send_message(bot_chatID2, newLinks)
try:
db.insert_many(item)
print(f'inserted {len(item)} articles')
except:
print('an error occurred quotes were not stored to db')
comparingProducts()
Я отправляю уведомление с помощью telebot в своем чате. Может быть, у меня проблема с неправильной работой с MongoDB, и я должен проверить вставки MongoDB?
Комментарии:
1. Я думаю, вы имеете в виду ‘очистку’. Утилизация означает выбрасывание.
2. Да) Извините, мой английский плохой)