python3 - 添加链接导入URL

时间:2017-06-14 06:37:38

标签: html python-3.x beautifulsoup

我想将网址添加到我收到的链接中。我怎样才能做到这一点? 我的源代码是这样的

</a></li><li><a href="/archive/2017-06-13">
        Link title 2017-06-13
      </a></li><li><a href="/archive/2017-06-12">
        Link title 2017-06-12
      </a></li><li><a href="/archive/2017-06-11">

我的python 3代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib.request
url = "https://myurl.com"
parseurl = "https://myurl.com/archivelist"
url_readd = urllib.request.urlopen(parseurl)
soup = BeautifulSoup(url_readd, 'html.parser')
project_href = [i['href'] for i in soup.find_all('a', href=True) if i['href'] != "#"]

for string in project_href:
  print(append url(string))

1 个答案:

答案 0 :(得分:0)

您正在寻找字符串连接(甚至可能是字符串格式化),有几种可能性:

for string in project_href:
    print(url + string)
    print("".join([url, string]))
    print("{}{}".format(url, string))
    print("%s%s" % (url, string))