社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python-沃尔玛的类别名称Web抓取

Wicaledon • 3 年前 • 1631 次点击  

我想从这个沃尔玛找到部门名称 link .你可以看到,首先,里面左边有7个部门 Departments (巧克力饼干、饼干、黄油饼干等)。当我点击 See All Departments ,增加了9个类别,因此现在的数字是16。我正试图让所有16个部门都自动。我写了这段代码;

from selenium import webdriver

n_links = []

driver = webdriver.Chrome(executable_path='D:/Desktop/demo/chromedriver.exe')
url = "https://www.walmart.com/browse/snacks-cookies-chips/cookies/976759_976787_1001391" 
driver.get(url)

search = driver.find_element_by_xpath("//*[@id='Departments']/div/div/ul").text
driver.find_element_by_xpath("//*[@id='Departments']/div/div/button/span").click()
search2 = driver.find_element_by_xpath("//*[@id='Departments']/div/div/div/div").text

sep = search.split('\n')
sep2 = search2.split('\n')

lngth = len(sep)
lngth2 = len(sep2)

for i in range (1,lngth):
    path = "//*[@id='Departments']/div/div/ul/li"+"["+ str(i) + "]/a"
    nav_links = driver.find_element_by_xpath(path).get_attribute('href')
    n_links.append(nav_links)
    
for i in range (1,lngth2):
    path = "//*[@id='Departments']/div/div/div/div/ul/li"+"["+ str(i) + "]/a"
    nav_links2 = driver.find_element_by_xpath(path).get_attribute('href')
    n_links.append(nav_links2)   
    
print(n_links)
print(len(n_links))

最后,当我运行代码时,我可以看到里面的链接 n_links 大堆但问题是;有时有13个链接,有时有14个。应该是16岁,我还没见过16岁,只有13或14岁。我试图补充 time.sleep(3) 之前 search2 线路,但不起作用。你能帮助我吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132829
 
1631 次点击  
文章 [ 4 ]  |  最新文章 3 年前