简体   繁体   English

Python for 循环没有遍历所有元素? 它只取第一个元素

[英]Python for loop not looping through all the elements? It is only taking the 1st element

I am trying to scrape the data from naukri.com, here I am trying to scrape the location details for each recruiter visible on the page.我正在尝试从 naukri.com 中抓取数据,在这里我正在尝试抓取页面上可见的每个招聘人员的位置详细信息。

The code I am writing is:我正在编写的代码是:

def sol7(url):
    driver = webdriver.Chrome(r'C:\Users\Rahul\Downloads\chromedriver_win32\chromedriver.exe')
    driver.maximize_window()
    driver.get(url)
    
    #getting link of recruiters
    recruiters_tag = driver.find_element_by_xpath("//ul[@class='midSec menu']/li[2]/a")
    driver.get(recruiters_tag.get_attribute('href'))
    
    #search and click for data scientist
    driver.find_element_by_xpath("//input[@class='sugInp']").send_keys('Data science')
    driver.find_element_by_xpath("//button[@id='qsbFormBtn']").click()
    
    
    highlight_table_tag = driver.find_elements_by_xpath("//p[@class='highlightable']")
    print(len(highlight_table_tag))
    for i in highlight_table_tag:
        try:
            print((i.find_element_by_xpath("//small[@class='ellipsis']")).text)
        except NoSuchElementException:
            print('-')

1st I have extracted all recruiters details in list highlight_table_tag.首先,我在列表 highlight_table_tag 中提取了所有招聘人员的详细信息。 highlight_table_tag includes all the elements on the page however the loop only takes the 0th element of my list. highlight_table_tag 包括页面上的所有元素,但是循环只占用我列表的第 0 个元素。 I want to scrape the location in such a way, if the location tag is not there in each element of highlight_table_tag then print '-' instead.我想以这种方式刮取位置,如果在 highlight_table_tag 的每个元素中都不存在位置标签,则改为打印'-'。

Please help!!!!请帮忙!!!!

While xpathing child elements use.虽然 xpathing 子元素使用。 in front of the xpath otherwise you will be getting it from root node.在 xpath 前面,否则您将从根节点获取它。 Which ends up with 1 element everytime.每次都有 1 个元素。

print((i.find_element_by_xpath(".//small[@class='ellipsis']")).text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM