Я хотел бы очистить веб-сайт, на данный момент я могу очистить веб-сайт со страницы 1 до 5.
Единственное, что меня беспокоит, это то, что на странице 3 веб-сайт меняет класс div на информацию, которую я хочу очистить.
от: "xl-ценовой диапазонЦена"
to: "l-цена диапазонаЦена"
to: "m-цена диапазонаЦена"
find("div", {"class": "xl-price rangePrice"})
Как я могу изменить этот код, чтобы запрос очищал "xl-price rangePrice" и "l-price rangePrice" и "m-price rangePrice"?
Заранее спасибо за ваши ответы!
Вот мой общий код:
#Fonctionne jusqu à la page 5 mais j'ai pas la page 5
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
options = Options()
options.add_argument("window-size=1400,600")
from fake_useragent import UserAgent
ua = UserAgent()
a = ua.random
user_agent = ua.random
print(user_agent)
options.add_argument(f'user-agent={user_agent}')
driver = webdriver.Chrome('/Users/raduulea/Documents/chromedriver', options=options)
driver.get('https://www.immoweb.be/fr/recherche/immeuble-de-rapport/a-vendre/liege/4000')
import time
time.sleep(10)
Title = []
address = []
price = []
surface = []
desc = []
page = 2
while True:
time.sleep(10)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
if int(page)<=2:
results = soup.find_all("div", {"class": "result-xl"})
for result in results:
Title.append(result.find("div", {"class": "title-bar-left"}).get_text().strip())
address.append(result.find("span", {"result-adress"}).get_text().strip())
price.append(result.find("div", {"class": "xl-price rangePrice"}).get_text().strip())
surface.append(result.find("div", {"class": "xl-surface-ch"}).get_text().strip())
desc.append(result.find("div", {"class": "xl-desc"}).get_text().strip())
if len(driver.find_elements_by_css_selector("a.next")) > 0:
url = "https://www.immoweb.be/fr/recherche/immeuble-de-rapport/a-vendre/liege/4000/?page={}".format(page)
driver.get(url)
page += 1
elif 3 <= int(page) < 5:
results = soup.find_all("div", {"class": "result-l"})
for result in results:
Title.append(result.find("div", {"class": "title-bar-left"}).get_text().strip())
address.append(result.find("span", {"result-adress"}).get_text().strip())
price.append(result.find("div", {"class": "l-price rangePrice"}).get_text().strip())
surface.append(result.find("div", {"class": "l-surface-ch"}).get_text().strip())
desc.append(result.find("div", {"class": "l-desc"}).get_text().strip())
if len(driver.find_elements_by_css_selector("a.next")) > 0:
url = "https://www.immoweb.be/fr/recherche/immeuble-de-rapport/a-vendre/liege/4000/?page={}".format(page)
driver.get(url)
page += 1
elif 5 <= int(page) <= 6:
results = soup.find_all("div", {"class": "result-m"})
for result in results:
Title.append(result.find("div", {"class": "title-bar-left"}).get_text().strip())
address.append(result.find("span", {"result-adress"}).get_text().strip())
price.append(result.find("div", {"class": "m-price rangePrice"}).get_text().strip())
surface.append(result.find("div", {"class": "m-surface-ch"}).get_text().strip())
desc.append(result.find("div", {"class": "m-desc"}).get_text().strip())
if len(driver.find_elements_by_css_selector("a.next")) > 0:
url = "https://www.immoweb.be/fr/recherche/immeuble-de-rapport/a-vendre/liege/4000/?page={}".format(page)
driver.get(url)
page += 1
else:
break
elif int(page) > 6:
break
df = pd.DataFrame({"Title": Title, "Address": address, "Price:": price, "Surface": surface, "Description": desc})
df.to_csv("immo_scrap.csv")
Если кому-то это нужно:
Я нашел другое решение, частично из ответов здесь, а также проверяя другие форумы:
Вот мой код, теперь намного проще:
#Fonctionne jusqu à la page 5 mais j'ai pas la page 5
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
options = Options()
options.add_argument("window-size=1400,600")
from fake_useragent import UserAgent
ua = UserAgent()
a = ua.random
user_agent = ua.random
print(user_agent)
options.add_argument(f'user-agent={user_agent}')
driver = webdriver.Chrome('/Users/raduulea/Documents/chromedriver', options=options)
driver.get('https://www.immoweb.be/fr/recherche/immeuble-de-rapport/a-vendre/liege/4000')
import time
time.sleep(10)
Title = []
address = []
price = []
surface = []
desc = []
page = 2
while True:
time.sleep(10)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
if int(page)<=6:
results = soup.find_all(True, {"class": ["result-xl", "result-l","result-m"]})
for result in results:
Title.append(result.find("div", {"class":"title-bar-left"}).get_text().strip())
address.append(result.find("span", {"result-adress"}).get_text().strip())
price.append(result.find("div", {"class": ["xl-price rangePrice", "l-price rangePrice", "m-price rangePrice"]}).get_text().strip())
surface.append(result.find("div", {"class": ["xl-surface-ch", "l-surface-ch", "m-surface-ch"]}).get_text().strip())
desc.append(result.find("div", {"class": ["xl-desc", "l-desc", "m-desc"]}).get_text().strip())
if len(driver.find_elements_by_css_selector("a.next")) > 0:
url = "https://www.immoweb.be/fr/recherche/immeuble-de-rapport/a-vendre/liege/4000/?page={}".format(page)
driver.get(url)
page += 1
elif int(page) > 6:
break
df = pd.DataFrame({"Title": Title, "Address": address, "Price:": price, "Surface": surface, "Description": desc})
df.to_csv("immoweb_no_secret.csv")