xpath了解亚马逊页面上的产品详细信息

时间:2019-03-18 14:55:40

标签: python-2.7 selenium xpath

我正在尝试在此页的第二列中获取产品详细信息:

https://www.amazon.co.uk/Philips-50PUS6753-12-Freeview-Ambilight/dp/B07C9GQSLV/ref=sr_1_1?ie=UTF8&qid=1552917671&sr=8-1&keywords=B07C9GQSLV

我正在使用以下xpath

://*[@id="productDetailsTable"]/tbody/tr/td/div[2]/ul/li/text()

代码:

if self.hasXpath('//*[@id="productDetailsTable"]/tbody/tr/td/div/ul/li/b'):
    prod_info_dt = []
    prod_info_dd = []                      
    prod_info_dt_list = self.browser.find_elements_by_xpath('//*[@id="productDetailsTable"]/tbody/tr/td/div/ul/li/b')
    prod_info_list_count = len(prod_info_dt_list)
    for curr_prod_dt_info in prod_info_dt_list:
        prod_info_dt.append(curr_prod_dt_info.get_attribute('innerHTML').encode('ascii','ignore').strip().replace('.','_'))
        prod_info_dd_list = self.browser.find_elements_by_xpath('//*[@id="productDetailsTable"]/tbody/tr/td/div[2]/ul/li/text')
        for curr_prod_dd_info in prod_info_dd_list:
            if curr_prod_dd_info.get_attribute('innerHTML').encode('ascii','ignore').strip()=='<i class="fi-x"></i>':
                prod_info_dd.append("No")
            elif curr_prod_dd_info.get_attribute('innerHTML').encode('ascii','ignore').strip()=='<i class="fi-check"></i>':
                prod_info_dd.append("Yes")
            else:
                prod_info_dd.append(curr_prod_dd_info.get_attribute('innerHTML').encode('ascii','ignore').strip())
    for x in range(prod_info_list_count):
        product_dict[prod_info_dt[x]] = prod_info_dd[x]   

1 个答案:

答案 0 :(得分:0)

我不确定您要看哪个值。如果您要照管产品说明,则应该使用xpath

elements=driver.find_elements_by_xpath("//div[@id='feature-bullets']/ul/li/span")
proddesc=[]
for ele in elements:
    print(ele.text)
    proddesc.append(ele.text)

print(proddesc)