joeyism / linkedin_scraper

A library that scrapes Linkedin for user data
GNU General Public License v3.0
1.86k stars 527 forks source link

UnboundLocalError: local variable 'work_times' referenced before assignment #162

Open dibyendu-chatterjee opened 1 year ago

dibyendu-chatterjee commented 1 year ago

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 -> https://github.com/joeyism/linkedin_scraper/pull/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

javaCR7 commented 1 year ago

also got that error .How to solve it?

mrunallachake commented 1 year ago

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

exuberant-coder commented 1 year ago

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.

derejehinsermu commented 12 months ago

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 10 months ago

This fix works. Can be pulled.

exast commented 9 months ago

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.