Я очищаю этот веб-сайт: https://news.ycombinator.com/jobs. У меня есть код, чтобы очистить веб-сайт и сохранить необходимую информацию в локальной базе данных. Информация, которую мне нужно почистить:
- Название компании, которая нанимает.
- Местоположение компании.
- Позиция, для которой предназначена реклама.
Мой вопрос: как улучшить мой скрипт для выполнения следующих задач?
- Периодически очищать сайт.
- Скребок должен очищать только новую информацию на сайте с момента последнего
раз он бежал.
import mysql.connector
from mysql.connector import errorcode
from bs4 import BeautifulSoup
import requests
url = "https://news.ycombinator.com/jobs"
response = requests.get(url, timeout=5)
content = BeautifulSoup(response.content, "html.parser")
table = content.find("table", attrs={"class":"itemlist"})
array = []
for elem in table.findAll("a", attrs={"class":"storylink"}):
array.append(elem.text)
try:
# open the database connection
cnx = mysql.connector.connect(user='root', password='mypassword',
host='localhost', database='scraping')
insert_sql = ('INSERT INTO `jobs` (`listing`) VALUES (%s)')
# get listing data
listing_data = array
# loop through all listings executing INSERT for each with the cursor
cursor = cnx.cursor()
for listing in listing_data:
print('Storing data for %s' % (listing))
cursor.execute(insert_sql, (listing,))
# commit the new records
cnx.commit()
# close the cursor and connection
cursor.close()
cnx.close()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print('Something is wrong with your username or password')
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print('Database does not exist')
else:
print(err)
else:
cnx.close()