126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
import requests
|
|
import string
|
|
import re
|
|
from bs4 import BeautifulSoup
|
|
import openpyxl
|
|
from openpyxl import load_workbook
|
|
import argparse
|
|
import os
|
|
|
|
def dir_path(path):
|
|
if os.path.isdir(path):
|
|
return path
|
|
else:
|
|
raise argparse.ArgumentTypeError(f"readable_dir:{path} is not a valid path")
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-f",type=str,default='redbook.xlsm')
|
|
parser.add_argument("-w",type=str,default='Sheet1')
|
|
parser.add_argument("-u")
|
|
parser.add_argument("-r",type=str,default='R961893')
|
|
args = parser.parse_args()
|
|
print(args)
|
|
|
|
# Making a GET request
|
|
uri = 'https://www.redweek.com/posting/' + str(args.r)
|
|
#uri = 'https://www.redweek.com/posting/' + str(args.r)
|
|
r = requests.get(uri)
|
|
# Parsing the HTML
|
|
soup = BeautifulSoup(r.content, 'html.parser')
|
|
|
|
w = soup.find(class_='total__price')
|
|
x = soup.find('h2', class_='price-value mb-1')
|
|
z = soup.find(class_='posting-main-price')
|
|
v = soup.find(class_='posting-feat-unit-view')
|
|
y = soup.find(class_='posting-main-title')
|
|
u = soup.find(class_='posting-feat-sleep-bedrooms')
|
|
u1 = soup.find(class_='posting-feat-sleep-guests')
|
|
u2 = soup.find(class_='posting-feat-sleep-beds')
|
|
all_links= soup.findAll('a')
|
|
|
|
#from lxml import etree
|
|
#dom = etree.HTML(str(soup))
|
|
#PropURL=(dom.xpath('//*[@id="main-content"]/div[2]/div/div[1]/div[1]/section/div[4]/header/h1/a')[0].text)# //*[@id="main-content"]/div[2]/div/div[1]/div[1]/section/div[4]/header/h1/a
|
|
#PropURLtext=PropURL
|
|
#PropURL = re.sub('<.*?>','',PropURL)
|
|
#PropURL = re.sub('(^.*? (?=<)<)','',PropURL)
|
|
|
|
|
|
#PropURL = all_links[65]
|
|
PropertyName = str(y.text).strip()
|
|
PricePerNight = str(x.text).strip()
|
|
ResortView = str(v.text).strip()
|
|
ResortView = ResortView.replace("View: ", "")
|
|
Bedrooms = str(u.text).strip()
|
|
Bedrooms = Bedrooms.replace("\n"," ")
|
|
SearchBedrooms = re.sub('\D', '', Bedrooms)
|
|
Sleeps = str(u1.text).strip()
|
|
Sleeps = Sleeps.replace("\n"," ")
|
|
SearchSleeps = re.sub('\D', '', Sleeps)
|
|
SearchView = re.sub(" ",'%20',ResortView)
|
|
LOS = ''
|
|
BedDetails = str(u2.text).strip()
|
|
BedDetails = BedDetails.replace("\n"," ")
|
|
CheckIn = ""
|
|
URL = str(y).strip()
|
|
URL = URL.replace("\n", "")
|
|
URL = URL.replace("<a href=\"", "<a href=\"https://www.redweek.com")
|
|
URL2 = URL.replace('<h1 class="posting-main-title"><a href="','')
|
|
URL2 = (URL2.split('#'))
|
|
URL3 = URL2[0]
|
|
URL3 = URL3 + '/timeshare-rentals?bedrooms='+ SearchBedrooms+ '&sleeps=' + SearchSleeps +'&available=1'
|
|
URL4 = URL3 + '&view='+SearchView
|
|
|
|
TotalPrice = str(w).strip()
|
|
TotalPrice = re.sub('<.*?>', '', TotalPrice)
|
|
Body1 = str(z.text).strip()
|
|
for line in Body1.splitlines():
|
|
# print (line)
|
|
if line == "\n" : next (line)
|
|
if line.startswith("Check-in:") :
|
|
CheckIn = line.replace("Check-in: ","")
|
|
if line.startswith("Check-out:") :
|
|
CheckOut = line.replace("Check-out: ","")
|
|
if re.findall('^\d-', line):
|
|
LOS = line.strip()
|
|
if line.startswith("Posted by") :
|
|
Owner= line.replace("Posted by ","")
|
|
|
|
print ('Property: ',PropertyName)
|
|
print ('Property URL: ',URL3)
|
|
print ('Bedrooms: ',Bedrooms)
|
|
print ('Sleeps : ',Sleeps)
|
|
print ('Nights: ',LOS)
|
|
print ('BedConfig: ',BedDetails)
|
|
print ('URL: ',URL)
|
|
print ('View: ',ResortView)
|
|
if CheckIn: print ('Check-in: ',CheckIn)
|
|
if CheckOut: print ('Check-out: ',CheckOut)
|
|
print ('Price Per Night: ',PricePerNight)
|
|
print ('TotalPrice: ', TotalPrice)
|
|
print ('Posted by: ', Owner)
|
|
|
|
|
|
location = str(args.f)
|
|
import xlwings as xw
|
|
#wb = xw.Book() # this will create a new workbook
|
|
#wb = xw.Book('FileName.xlsx') # connect to a file that is open or in the current working directory
|
|
#wb = xw.Book(r'C:\path\to\file.xlsx') # on Windows: use raw strings to escape backslashes
|
|
wb = xw.Book(location) # connect to a file that is open or in the current working directory
|
|
sheet = wb.sheets[args.w]
|
|
sheet.range('B7').value = PropertyName
|
|
sheet.range('B8').value = Bedrooms
|
|
sheet.range('B9').value = Sleeps
|
|
sheet.range('B10').value = LOS
|
|
sheet.range('B11').value = BedDetails
|
|
sheet.range('B12').value = URL
|
|
sheet.range('B13').value = ResortView
|
|
sheet.range('B14').value = CheckIn
|
|
sheet.range('B15').value = CheckOut
|
|
sheet.range('B16').value = PricePerNight
|
|
sheet.range('B17').value = TotalPrice
|
|
sheet.range('B18').value = Owner
|
|
sheet.range('B34').value = str(PropURL)
|
|
sheet.range('B35').value = URL4
|
|
sheet.range('B36').value = URL3
|
|
#wb.save |