早安各位。
我正在從本地物業公司抓取一些資料,然后將資料放入本地 mysql 資料庫中。我想做的是將一個欄位(close_date)與當前日期進行比較。一旦關閉日期現在或已經過去,請將關閉日期更改為 0000-00-00 或 Null。
我計劃在某個時候僅在我本地的網頁上顯示資料,以上是在 Python 中做我想做的事情的最佳方式,還是在顯示資料時最好實作。
from bs4 import BeautifulSoup
import requests
import mysql.connector
from datetime import datetime
web_link_list = []
class House:
def __init__(self, address, advert, postcode, area, prop_type, opening_date, closing_date, bedrooms, weblink):
self.address = address
self.advert = advert
self.postcode = postcode
self.area = area
self.prop_type = prop_type
self.opening_date = opening_date
self.closing_date = closing_date
self.bedrooms = bedrooms
self.weblink = weblink
def insert_sql(self):
my_db = mysql.connector.connect(host='localhost',
user='test1',
password='testpass',
database='Houses')
my_cursor = my_db.cursor()
sql = "insert ignore into tb1 (address, advert, postcode, area, property_type, opening_date, closing_date, bedrooms, weblink) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (self.address, self.advert, self.postcode, self.area, self.prop_type, self.opening_date, self.closing_date, self.bedrooms, self.weblink)
my_cursor.execute(sql, val)
my_db.commit()
print(my_cursor.lastrowid, " Inserted")
def get_links():
url = 'https://angushomefinder.homeconnections.org.uk/my-cbl/property-search'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
for item in soup.find_all('a', attrs={'class': 'blue-btn'}, href=True):
web_link_list.append('https://angushomefinder.homeconnections.org.uk' item['href'])
def get_house():
for item in web_link_list:
page = requests.get(item)
soup = BeautifulSoup(page.content, 'html.parser')
result = soup.find('table', class_="tableproperty")
new_house = House(soup.find('address', {'class': 'address-box'}).text.title(),
result.find('td', attrs={'data-title': 'Advert'} if result.find('td', attrs={'data-title': 'Advert'}) else None).text,
result.find('td', attrs={'data-title': 'Post code'} if result.find('td', attrs={'data-title': 'Post code'}) else None).text,
result.find('td', attrs={'data-title': 'Area'} if result.find('td', attrs={'data-title': 'Area'}) else None).text.title(),
result.find('td', attrs={'data-title': 'Property type'} if result.find('td', attrs={'data-title': 'Property type'}) else None).text.title(),
datetime.strptime(result.find('td', attrs={'data-title': 'Opening date'} if result.find('td', attrs={'data-title': 'Opening date'}) else None).text, '%d/%m/%Y'),
datetime.strptime(result.find('td', attrs={'data-title': 'Closing date'} if result.find('td', attrs={'data-title': 'Closing date'}) else None).text, '%d/%m/%Y'),
result.find('td', attrs={'data-title': 'Bedrooms'} if result.find('td', attrs={'data-title': 'Bedrooms'}) else None).text,
item)
House.insert_sql(new_house)
get_links()
get_house()
uj5u.com熱心網友回復:
處理現有的行。– pr0xibus
如果要修改值之間的距離為 8 天或更長時間的行中的值,則
UPDATE table_name
SET closing_date = NULL
WHERE DATEDIFF(closing_date, opening_date) >= 8
-- AND closing_date IS NOT NULL
如果我不正確地理解您的標準,請調整條件(使用正確的比較運算子)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426933.html
下一篇:我無法列印用硒報廢的資料
