Home > Software design >  TypeError: can only concatenate str (not "list") to str error while webscraping with Pytho
TypeError: can only concatenate str (not "list") to str error while webscraping with Pytho

Time:12-18

I have read a lot of things about this type of error. However, I still couldn't figure how to solve it out. I am webscraping a website with Python and trying to reach another website through the href links.

years_code=['114','115','116']
base_url = 'https://www.congress.gov/search?q={"source":"legislation","congress":'
base_url_link = 'https://www.congress.gov'
links_array = []
for year in years_code:
  
  main_url = base_url   year   '}'
  html_page = requests.get(main_url)
  soup = BeautifulSoup(html_page.text, 'html.parser')
  allData = soup.findAll('li',{'class':'expanded'})

  # getting the links for cosponsor
  cosponsors_link=[x.find('strong', string='Cosponsors:').findNext('a',href=True).get('href') for x in allData]
  links_array.append(cosponsors_link)
  for link in links_array:

    main_url_link = base_url_link   link
    html_page_link = requests.get(main_url_link)
    soup_link = BeautifulSoup(html_page_link, 'html.parser')
    allData_link = soup_link.findAll('li',{'class':'facetbox-shownrow'})
  
    district = [list(x.stripped_strings)[0] for x in soup_link.select('li.facetbox-shownrow a')]
    district_array.append(district)


district_array
end 

However, when I run this code I am getting the error "TypeError: can only concatenate str (not "list") to str" Before the last for loop, when I display "links_array", I am getting the output but in the for loop something is wrong.

CodePudding user response:

cosponsors_link and links_array are all list of strings so you need to extend links_array by cosponsors_link. so replace this

links_array.append(cosponsors_link)

by

links_array.extend(cosponsors_link)

finally it seems like globaly your code is not very efficient

  • Related