joeyism/linkedin_scraper

UnboundLocalError: local variable 'work_times' referenced before assignment

dbnduchatrji opened this issue · 6 comments

Hello Team,

I am getting this error below ->
/linkedin_scraper/linkedin_scraper/person.py", line 147, in get_experiences
times = work_times.split("·")[0].strip() if work_times else ""
UnboundLocalError: local variable 'work_times' referenced before assignment

Found this problem while testing PR 158 -> #158

Looks like it happens when the LinkedIn profile being scrapped has an experience without the start/end dates.

Thank you for the help

Regards
DC

also got that error .How to solve it?

I'm also facing the same issue. What is the solution for this?

Hey,
I am also facing same issue. Please let me know whether it is feasible to work on it or not. Strange part is that the code is working absolutely fine for maximum profile ids but for rest of them getting same error.

I have resolved the issue by fixing the using try and except and ensuring that the variable is properly defined within the given loop. This adjustment resolves the UnboundLocalError and allows the code to execute without any errors.

def get_experiences(self):
# ...

for position in main_list.find_elements(By.XPATH, "li"):
    # ...

    if len(outer_positions) == 4:
        position_title = outer_positions[0].find_element(By.TAG_NAME, "span").text
        # Rest of the code

    elif len(outer_positions) == 3:
        if "·" in outer_positions[2].text:
            position_title = outer_positions[0].find_element(By.TAG_NAME, "span").text
            # Rest of the code
        else:
            position_title = ""
            # Rest of the code

    # Indentation started from here to end of the function.
 try:
    times = work_times.split("·")[0].strip() if work_times else ""
    duration = work_times.split("·")[1].strip() if len(work_times.split("·")) > 1 else None
   except:
    pass
    # Rest of the code
    else:
            description = position_summary_text.text if position_summary_text else ""
        try:
            experience = Experience(
                position_title=position_title,
                from_date=from_date,
                to_date=to_date,
                duration=duration,
                location=location,
                description=description,
                institution_name=company,
                linkedin_url=company_linkedin_url
            )
            self.add_experience(experience)
            except:
                       pass
            
# Rest of the code
Xses-1 commented

This fix works. Can be pulled.

exast commented

I tried using @derejehinsermu 's fix. However, this fix leaves the objects empty and does not capture the details of the given experience from the profile. I went a different way and added a new scenario of "outer_positions" with lenght = 2.
I also created a Pull Request for the fix. I tried with different profiles and seems to work.