[英]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 我有一个set()相对链接,需要将其组合到基本链接中,以使其成为绝对链接
This is the code that is creating the set() with the relative link. 这是使用相对链接创建set()的代码。 I want to combine all with the base link ex:"https:\\www.census.gov" 我想将所有内容与基本链接(例如:“ 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.