簡體   English   中英

將文字中的基本URL與一組中的相對URL組合

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

我有一個set()相對鏈接,需要將其組合到基本鏈接中,以使其成為絕對鏈接

這是使用相對鏈接創建set()的代碼。 我想將所有內容與基本鏈接(例如:“ 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'))

只需將基本鏈接設置為變量並添加字符串即可。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM