I am trying to move to the next page till the 'next' button exists at this link 'https://www.cbp.gov/contact/find-broker-by-port/4901?page=1'. I realized that the requests response doesn't have the button in it hence BeautifulSoup cannot find it. I tried adding headers/user-agent to requests but the element still doesn't appear. As far as I can tell, there is no Javascript generating content on this page. Here is the code. What am I missing?
def second_links(second_links_list=[], page2_num=0):
try:
with open('port.csv', 'r') as read_obj:
csv_reader = reader(read_obj)
for row in csv_reader:
row = row[-1]
page2 = requests.get(row.format(page2_num))
soup2 = BeautifulSoup(page2.content, 'html')
results2 = soup2.find(id='region-content')
table2cells = results2.find_all('td', class_='views-field views-field-title views-align-center')
for cell in table2cells:
cell2link = cell.find('a', href=True)
second_links_list.append('https://www.cbp.gov'+cell2link['href'])
next2_page = results2.find('li', class_='pager-next')
if next2_page:
page2_num += 1
second_links(second_links_list, page2_num)
return second_links_list
except requests.exceptions.ConnectionError:
page2.status_code = 'connection refused'
