Как вы получаете селен вебдрайвер, чтобы вернуть весь HTML с сайта? - PullRequest
1 голос
/ 23 марта 2019

Я пытаюсь очистить списки недвижимости от https://www.utahrealestate.com/search/map.search/page/1, и мне не удается заставить веб-драйвер селена очистить все html.

Из того, что я могу сказать, сайт использует функцию javascript для динамической загрузки списков на карте.

Вместо того, чтобы возвращать HTML-код, содержащий нужные данные под тегом, он возвращает что-то вроде этого:

<div id="results-listings">
<div style="height: 400px;"></div>
</div>
</div>
</div>
<!--right ad zone-->
<div class="advert-160-600 advert-right-zone" data-google-query-id="CKDYtP2Ol-ECFVAMswAd7vcDAg" id="div-gpt-ad-1533933823557-0" style="">
<div id="google_ads_iframe_/21730996110/UtahRealEstate/ListingResults/Right-Side-160x600_0__container__" style="border: 0pt none; display: inline-block; width: 160px; height: 600px;"><iframe data-google-container-id="1" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="600" id="google_ads_iframe_/21730996110/UtahRealEstate/ListingResults/Right-Side-160x600_0" marginheight="0" marginwidth="0" name="" sandbox="allow-forms allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" scrolling="no" src="https://tpc.googlesyndication.com/safeframe/1-0-32/html/container.html" style="border: 0px; vertical-align: bottom;" title="3rd party ad content" width="160"></iframe></div></div>
<div id="map_notification"></div>
<div id="map_markers_container" style="display: none;"></div>
</div>
</div>
<div class="advert-728-90" data-google-query-id="CKHYtP2Ol-ECFVAMswAd7vcDAg" id="div-gpt-ad-1533933779531-0" style="margin-top: 15px">
<div id="google_ads_iframe_/21730996110/UtahRealEstate/ListingResults/Center-Below-Map-728x90_0__container__" style="border: 0pt none;"><iframe data-google-container-id="2" data-load-complete="true" frameborder="0" height="90" id="google_ads_iframe_/21730996110/UtahRealEstate/ListingResults/Center-Below-Map-728x90_0" marginheight="0" marginwidth="0" name="google_ads_iframe_/21730996110/UtahRealEstate/ListingResults/Center-Below-Map-728x90_0" scrolling="no" srcdoc="" style="border: 0px; vertical-align: bottom;" title="3rd party ad content" width="728"></iframe></div></div>
<div class="container" style="margin-top: 20px;">
<p style="margin: 20px 0 40px 0;">UtahRealEstate.com is Utah's favorite place to find a home. MLS Listings are provided by the Wasatch Front Regional Multiple Listing Service, Inc., which is powered by Utah's REALTORS®. UtahRealEstate.com offers you the most complete and current property information available. Browse our website to find an accurate list of homes for sale in Utah and homes for sale in Southeastern Idaho.</p>
<h5>Find Utah Homes for Sale by City</h5>
<div class="row">
<div class="col-sm-7 five-three">
<div class="row">
<div class="col-sm-4">
<b><a href="/davis-county-homes">Davis County</a></b>
<ul>
<li><a href="/bountiful-homes">Bountiful</a></li>
<li><a href="/clearfield-homes">Clearfield</a></li>
<li><a href="/clinton-homes">Clinton</a></li>
<li><a href="/layton-homes">Layton</a></li>
<li><a href="/kaysville-homes">Kaysville</a></li>
<li><a href="/north-salt-lake-homes">North Salt Lake</a></li>
<li><a href="/south-weber-homes">South Weber</a></li>
<li><a href="/syracuse-homes">Syracuse</a></li>
<li><a href="/woods-cross-homes">Woods Cross</a></li>

Мой текущий код выглядит так:

from selenium import webdriver
from bs4 import BeautifulSoup as soup

utahRealEstate = 'https://www.utahrealestate.com/search/map.search/page/1'
browser = webdriver.Chrome()
page = browser.get(utahRealEstate)
innerHTML = browser.execute_script("return document.body.innerHTML")

page_soup = soup(innerHTML)
page_soup

Мне действительно нужна информация, которая содержится в классах "lists-info-left-col" и "lists-info-right-col".

Я очень новичок в этом, поэтому, пожалуйста, постарайтесь как можно дольше излагать ваше объяснение. Я ценю любую помощь!

Ответы [ 2 ]

0 голосов
/ 23 марта 2019

Далее рассчитывается информация о разбиении на страницы (чтобы быть более гибкой в ​​случае изменения информации о разбиении на страницы) и циклически повторяются все страницы доступных результатов. Он извлекает информацию о цене, адресе собственности и информации о свойствах в список списков, который сведен, преобразован в массив данных и записан в csv. Регулярное выражение используется, чтобы привести в порядок выходную информацию. Используются условия ожидания для получения информации.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re
import math
from bs4 import BeautifulSoup as bs
import pandas as pd

def getInfo(html): #function to return price and other listing info for the current page. Accepts the page source html as parameter
    soup = bs(html, 'lxml')
    items = soup.select('.inline_info')
    rowsToReturn = []
    for item in items:
        data = item.select('.list-info-content') #list containing address info and property details e.g. baths, beds
        price = item.select_one('h3').text.strip()
        address = re.sub('\s\s+', ' ',  data[0].text.strip()) #replace 2+ white space with single space
        propertyInfo = re.sub('\s\s+', ' ',  data[1].text.strip())
        rowToReturn = [price, address, propertyInfo]
        rowsToReturn.append(rowToReturn)
    return rowsToReturn

url = 'https://www.utahrealestate.com/search/map.search/page/1' #landing page
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".list-info-content"))) #wait for all listings content

reg = re.compile(r'(\d+)') #regex pattern looking for 1 or more numbers to be applied to class view-results which has the pagination and total results info
matches = reg.findall(driver.find_element_by_css_selector('.view-results').text) # [1,50,500] from 1 to 50 of 500
numResults = int(matches[2])
resultsPerPage = int(matches[1])
numPages = math.ceil(numResults/resultsPerPage)

results = []
results.append(getInfo(driver.page_source)) #add page one results

if numPages > 1: 
    for page in range(2, numPages + 1): #loop calculated number of pages 
        driver.get('https://www.utahrealestate.com/search/map.search/page/{}'.format(page)) #add new page number into url
        WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".list-info-content"))) #wait for all listings content
        results.append(getInfo(driver.page_source)) #add next page results

#flatten list of lists
finalList = [item for sublist in results for item in sublist]

df = pd.DataFrame(finalList, columns = ['price', 'address', 'property details']) #convert to dataframe and write to csv
df.to_csv(r'C:\Users\User\Desktop\Data.csv', sep=',', encoding='utf-8-sig',index = False )
driver.quit()

Пример результатов:

enter image description here

0 голосов
/ 23 марта 2019

Этот код начинается с первой страницы, анализирует его на предмет подробностей и затем загружает остальные страницы по одной, разбирая их на предмет подробностей, пока не останется больше страниц.Вы можете уточнить его, если хотите, в соответствии с вашими потребностями.

from selenium import webdriver
from bs4 import BeautifulSoup
import time
from selenium.common.exceptions import NoSuchElementException

utahRealEstate = 'https://www.utahrealestate.com/search/map.search/page/1'
browser = webdriver.Chrome()
page = browser.get(utahRealEstate)


# parse the page
def parse(html):
    soup = BeautifulSoup(html, 'html.parser')
    for i in soup.find_all('div', {'class': 'listings-info'}):
        print(i.get_text())


while True:
    try:
        # parse the current page.
        time.sleep(3)
        parse(browser.page_source)
        # Find the next page button and click it.
        browser.find_element_by_xpath("//a[text()='Next ']").click()
    except NoSuchElementException:
        # Couldn't find a next page button must have got to the end.
        break

browser.quit()

Выходы:

$615,000
3217 W 10305 S
South Jordan, UT 84095


5Beds
5Baths
4002Sq.Ft.
#1588082

Domain Real Estate LLC
...
...