如何找到一个字母变化的div类?

时间:2019-05-19 05:09:50

标签: python python-3.x web-scraping find

我想抓取一个网站,目前,我可以从第1页到第5页抓取该网站。 令我烦恼的是,在第3页上,网站从我要抓取的信息中更改了div类。

来自:“ xl-price rangePrice”

至:“ l-price rangePrice”

至:“ m-price rangePrice”

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")

2 个答案:

答案 0 :(得分:0)

您可以使用带选择符的CSS结束符($ =):

  

div [class $ =“-price rangePrice”]

答案 1 :(得分:0)

以下任何一个选择器都可以解决问题。尝试之前,请确保您已通过该Recaptcha障碍:

这个:

for item in soup.select("[data-type='resultgallery-resultitem'] .rangePrice"):
    print(item.text)

或者这个:

for item in soup.select("[class^='result-'] .rangePrice"):
    print(item.text)

这是方法之一:

from selenium import webdriver
from bs4 import BeautifulSoup

url = "https://www.immoweb.be/fr/recherche/immeuble-de-rapport/a-vendre/liege/4000?page=3"

with webdriver.Chrome() as driver:
    driver.delete_all_cookies()
    driver.get(url)
    soup = BeautifulSoup(driver.page_source,"lxml")
    for item in soup.select("[class^='result-'] .rangePrice"):
        print(item.text.strip())