Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 有没有办法更新以';开头的url/';用蟒蛇和靓汤_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 3.x 有没有办法更新以';开头的url/';用蟒蛇和靓汤

Python 3.x 有没有办法更新以';开头的url/';用蟒蛇和靓汤,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,以下是我面临的问题锚定标记href以/abcd.html开始 我想将其更新为href=”https://www.example.com/abcd.html“在美化组实例本身中 这是否可能?更改的href参数 ''' soup=BeautifulSoup(txt,'html.parser') #查找链接, from bs4 import BeautifulSoup txt = ''' <html> <a href="/abcd.html" title="link">So

以下是我面临的问题锚定标记href/abcd.html开始 我想将其更新为href=”https://www.example.com/abcd.html“美化组实例本身中


这是否可能?

更改
href
参数 ''' soup=BeautifulSoup(txt,'html.parser') #查找链接,
from bs4 import BeautifulSoup

txt = '''
<html>
<a href="/abcd.html" title="link">Some link</a>
</html>
'''

soup = BeautifulSoup(txt, 'html.parser')

# find link, <a> tag
link = soup.find('a')

# change href attribute in <a>
link['href'] = 'https://www.example.com' + link['href']

# print soup
print(soup.prettify())
<html>
 <a href="https://www.example.com/abcd.html" title="link">
  Some link
 </a>
</html>