我正在尝试从这个网页中提取一些属性。url='http://m.search.allheart.com/?q=stethoscope‘
我为这个-编写了以下xpath:
XPATH,ATTRIBUTE='XPATH','ATTRIBUTE'
NUM_RESULTS='NUM_RESULTS'
URL='URL'
TITLE='TITLE'
PROD_ID='PROD_ID'
IS_SALE='IS_SALE'
CURRENCY='CURRENCY'
REGULAR_PRICE='REGULAR_PRICE'
SALE_PRICE='SALE_PRICE'
conf_key={
NUM_RESULTS : {XPATH :'//div[@id="sort-page"]//div[@id="options" and @class="narrowed"]//hgroup[@id="sort-info" and @class="clearfix"]/h2', ATTRIBUTE:''} ,
URL : {XPATH:'//span[@class="info"]//span[@class="swatches clearfix product-colors"]//span[@class="price"]',ATTRIBUTE:'href'} ,
TITLE : {XPATH:'//div[@id="sort-results"]//li[@class="item product-box"]//span[@class="info"]//span[@class="title"]',ATTRIBUTE:''} ,
PROD_ID : {XPATH:'//div[@id="sort-results"]//li[@class="item product-box"]//span[@class="info"]//span[@class="swatches clearfix product-colors"]',ATTRIBUTE:'id'} ,
IS_SALE : {XPATH :'//div[@id="sort-results"]//li[@class="item product-box sale"]', ATTRIBUTE:''} ,
REGULAR_PRICE : {XPATH :'//div[@id="sort-results"]//li[@class="item product-box"]//span[@class="info"]//span[@class="price"]' , ATTRIBUTE:''} ,
SALE_PRICE : {XPATH :'//div[@id="sort-results"]//li[@class="item product-box sale"]//span[@class="info"]//span[@class="price"]' , ATTRIBUTE: '' } ,
}
chromedriver = "/usr/local/CHROMEDRIVER"
desired_capabilities=DesiredCapabilities.CHROME
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver,desired_capabilities=desired_capabilities)
driver.get(url)其思想是从第一个搜索页面中提取属性,以获取名称、网址、标题、正常价格和销售价格。
跳过剩下的代码。然后通过for循环提取文本。当我试着把这些东西打折的时候,
driver.find_elements_by_xpath(conf_key[SALE_PRICE][XPATH])
driver.find_elements_by_xpath(conf_key[REGULAR_PRICE][XPATH])然而,给了我,regular_price,sale_price,is_sale as‘5.98’,'$5.98','$24.98','$3.98','$6.98','$13.98','$24.98','$19.98',‘18.98’,'$3.98','$5.98','$24.98','$12.98',‘24.98’‘1,1,1,1 '$24.98’
而我想要-:
['$5.98', '$5.98', '$24.98','$49.99', '$3.98', '$6.98', '$13.98', '$24.98', '$19.98', '$18.98', '$3.98', '$5.98', '$96.99', '$24.98', '$12.98', '$24.98']
['','', '24.98', '' , '' ....]
[0, 0, 1, 0 , 0 ...]问答-: 我想强迫驱动程序返回'‘(或任何占位符),这样我就可以得到该产品不在销售的信号。网页将有类-:“项目产品-框”或“项目产品-框-销售”。
另外,我不想硬编码这一点,因为我需要对一组网页重复这个逻辑。我怎样才能做得更好,而不通过李,li1 ..诸若此类。是否有任何方法可以指示类在顺序扫描时不存在?
使用上面定义的,我确实将容器的其余部分正确地作为-:
SEARCH_PAGE
244 Items ['ah426010', 'ahdst0100', 'ahdst0500blk', 'ahd000090', 'ahdst0600', 'pms1125', 'ahdst0400bke', 'ahdst0400blk', 'adc609', 'ma10448', 'ma10428', 'pm121', 'pm108', 'pm122'] ['allheart Discount Dual Head Stethoscope', 'allheart Discount Single Head Stethoscope', 'allheart Cardiology Stethoscope', 'allheart Disposable Stethoscope', 'allheart Discount Pediatric / Infant Stethoscope With Interchangeable Heads Stethoscope', 'Prestige Medical Ultra-Sensitive Dualhead Latex Free Stethoscope', 'allheart Smoke Black Edition Clinical Stainless Steel Stethoscope', 'allheart Clinical Stainless Steel Stethoscope', 'ADC Adscope-Lite 609 Lightweight Double-Sided Stethoscope', 'Mabis Dispos-A-Scope Nurse Stethoscope', 'Mabis Spectrum Nurse Stethoscope', 'Prestige Medical Clinical Lite Stethoscope', 'Prestige Medical Dual Head Stethoscope', 'Prestige Medical Sprague Rappaport Stethoscope'] 我需要得到相同长度的列表,对应于每一个列表,用于正常和销售价格(和is_sale标志)。
发布于 2015-07-11 20:44:50
find_elements_by_X返回一个WebElements列表,他们每个人都可以称之为find_elements_by_X。
我发现css选择器比xpath IMO更容易阅读。尝试使用(F12) +右键单击+复制CSS路径。https://selenium-python.readthedocs.org/locating-elements.html#locating-elements-by-css-selectors
https://stackoverflow.com/questions/27540558
复制相似问题