简体   繁体   中英

Combining Base URL in literals with Relative URLS that are in a set

I have a set() of relative links that I need to combine to a base link so that it becomes an absolute link

This is the code that is creating the set() with the relative link. I want to combine all with the base link ex:"https:\\www.census.gov"

linker_set = set() 
for link in soup.find_all('a', attrs={'href': re.compile("^/")}):         
    print(link.get('href')) 
    linker_set.add(link.get('href'))

Just set the base link as a variable and add the strings.

base_url = 'https://www.census.gov'

linker_set = set() 
for link in soup.find_all('a', attrs={'href': re.compile("^/")}):
    print(link.get('href')) 
    # Store link string as variable
    href_link = link.get('href')
    # Add base url to href link
    new_link = base_url + href_link
    linker_set.add(new_link)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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