私はウェブサイトからいくつかのデータをスクレイピングしています、そして時には彼らは走行距離を表示し、他の回は、彼らがここで車両の説明 にMPGの表示私はXPathを使用していたとしようとしていたHTMLPythonのセレンスクレイピング一貫性のないフィールド
です単にここでオーダー
に行く関連する部分である:
def init_driver():
options = webdriver.ChromeOptions()
options.binary_location = '/usr/bin/google-chrome-stable'
options.add_argument('headless')
options.add_argument('window-size=1200x600')
driver = webdriver.Chrome(chrome_options=options)
driver.wait = WebDriverWait(driver, 5)
return driver
def scrape(driver):
#Tymm = year make model All three attributes are in the Header, Parse and separate before insterting to SQL
ymm_element = driver.find_elements_by_xpath('//*[@id="compareForm"]/div/div/ul/li/div/div/h3')
engine_element = driver.find_elements_by_xpath('//*[@id="compareForm"]/div/div/ul/li/div/div/div[3]/dl[1]/dd[1]')
trans_element = driver.find_elements_by_xpath('//*[@id="compareForm"]/div/div/ul/li/div/div/div[3]/dl[1]/dd[2]')
milage_element = driver.find_elements_by_xpath('//*[@id="compareForm"]/div/div/ul/li/div/div/div[3]/dl[1]/dd[3]')
要素の順序は、すべての車両のために同じではないので、私はそれは私が望んでタイトルの後にテキストを取得することができますので、それを記述する必要があります。ここで
は、要素の検査からクロームにHTMLをコピーした後、HTMLです:
<div class="description">
<dl> <dt>Engine:</dt> <dd>2.5L I-5 cyl<span class="separator">,</span>
</dd> <dt>Transmission:</dt> <dd>Manual<span class="separator">,</span></dd> <dt>Mileage:</dt> <dd>37,171 miles<span class="separator">,</span></dd> <dt>MPG Range:</dt> <dd>22/31<span class="separator">,</span></dd></dl><dl class="last"> <dt>Exterior Color:</dt> <dd>Reflex Silver Metallic<span class="separator">,</span></dd> <dt>Interior Color:</dt> <dd>Titan Black<span class="separator">,</span></dd> <dt>Stock #:</dt> <dd>P3229</dd></dl> <span class="ddc-more">More<span class="hellip">…</span></span>
<div class="calloutDetails">
<ul class="list-unstyled">
<li class="certified" style="margin-bottom: 10px;"><div class="badge "><img class="align-center" src="https://static.dealer.com/v8/global/images/franchise/white/en_US/logo-certified-volkswagen.gif?r=1356028132000" alt="Certified"></div></li><li class="carfax" style="margin-bottom: 10px;"><a href="http://www.carfax.com/cfm/ccc_displayhistoryrpt.cfm?partner=DLR_3&vin=3VWHX7AT1EM600723" class="badge carfax-one-owner pointer" target="_blank"><img class="align-center" src="https://static.dealer.com/v8/global/images/franchise/white/logo-certified-carfax-one-owner-lrg.png?r=1405027620000" alt="Carfax One Owner"></a></li>
</ul>
</div>
<div class="hproductDynamicArea"></div>
</div>
基本的に私が代わりにXPathをナンバリングタイトルの後にテキストを検索する必要があります。あなたが使用することができたXPathと
マイ年の製造元とモデルが同じ要素」タグですべてです、あなたは正しい方向に私を指すでしまたはライブラリのスプリットヘッドを示唆
ありがとう、私はそうだろう、私は二重引用符に変更しなければならなかったが、それは魔法のように動作します。私は矛盾を避けるために、それぞれのCarを1つずつループさせます。 –
もう一度迷惑して申し訳ありません。デフスクレイプ(ドライバー): cars = driver.find_elements_by_xpath( '// div [@ class = "description"]') ( "(dt [contains(text()、(Engine)] /後続兄弟:: dd") mileage = car.find_element_by_xpath( "// 、 'マイレージ')] /追従兄弟:: dd ") print(mileage.text、engine.text) –
def scrape(ドライバ): cars = driver.find_elements_by_xpath( '// div [@ class ="車の中の車のための : エンジン= car.find_element_by_xpath( "//dtは(テキスト()、エンジン 'を含む)] /追従兄弟:: dd") mileage = car.find_element_by_xpath( "//dt [contains(text()、 'Mileage')]/following-sibling :: dd") print(mileage.text、engine.text) –