2017-06-20 52 views
0

我从一个网站刮的一些数据,有时他们显示milage,其他时间就是在车辆描述 这里显示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&amp;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的标题后要搜索的文字。

我一年的品牌和型号都在同一个元素“标签,你能指出我在正确的方向或建议库驳头

回答

0

首先,使用XPath可以使用包含,像这样:

driver.find_elements_by_xpath('//dt[contains(text(),'Engine')]') 

它看起来更清洁,更容易使用和更强大的

二,阅读下面的XPath,兄弟姐妹,前同辈,父母和祖先它会帮助你建立整齐的XPath定位:。

driver.find_elements_by_xpath('//dt[contains(text(),'Engine:')]/following-sibling::dd') 
driver.find_elements_by_xpath('//dt[contains(text(),'Transmission:')]/following-sibling::dd') 
driver.find_elements_by_xpath('//dt[contains(text(),'Mileage:')]/following-sibling::dd') 

以上的XPath将工作无论哪个命令你的HTML元素都位于。

+0

谢谢你,我会这么做,我只好换到双引号,但它的工作原理就像一个魅力。我也会逐一循环每辆车,以避免出现差异。 –

+0

对不起,你再次通过Web元素循环浏览:def scrape(driver): cars = driver.find_elements_by_xpath('// div [@ class =“description”]') 汽车中的汽车: milestone = car.find_element_by_xpath(“// dt [contains(text()())包含(text(),'Engine')]/following-sibling :: dd”) mileage = car.find_element_by_xpath ,'Mileage')]/following-sibling :: dd“) print(mileage.text,engine.text) –

+0

def scrape(driver): cars = driver.find_elements_by_xpath('// div [@ class =”描述“]') 汽车中的汽车: engine = car.find_element_by_xpath(”// dt [contains(text(),'Engine')]/following-sibling :: dd“) mileage = car.find_element_by_xpath(“// dt [contains(text(),'Mileage')]/following-sibling :: dd”) print(mileage.text,engine.text) –