这是我在 amazon.com 结果页面上的测试代码(“ipad”搜索)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(r'c:\chromedriver\chromedriver.exe', options=options)
url = 'https://www.amazon.com/s?k=ipad&ref=nb_sb_noss'
driver.get(url)
items = driver.find_elements_by_xpath('//div[@data-component-type="s-search-result"]')
print('items::', items)
输出
<块引用>items:: [
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(r'c:\chromedriver\chromedriver.exe', options=options)
url_ebay = 'https://www.ebay.com/sch/i.html?_nkw=ipad&rt=nc&_ipg=200&_pgn=2'
driver.get(url_ebay)
items = driver.find_element_by_xpath('//div[contains(@class, "s-item__info") and contains(@class, "clearfix")]')
print('items::', items)
有两个类名(s-item__info,clearfix),所以我用
"//div[contains(@class, "s-item__info") and contains(@class, "clearfix")]"
但输出是单一的。
<块引用>items::
如何获取所有产品元素?
答案 0 :(得分:0)
您输入错误:find_elements_by_xpath。
find_element_by_xpath
返回第一个匹配项,而
find_elements_by_xpath
返回所有匹配项。