在中文中,使用urlparse.urljoin而不是os.path.join来连接Python的URL
由于在Python中从csv文件中获取URL列表并将这些URL进行连接时遇到了问题,我做了以下笔记。
路径拼接(os.path.join)没有成功。
import os.path
channel_urls = ['/channel/UCVrJcoR8hUN9Rn7uPI4z6NQ']
for i in channel_urls:
root_url = 'https://www.youtube.com'
channel_url = ('%s' % i)
channel_about_url = urlparse.urljoin(youtube_url, channel_url, 'about')
print(self.open_channel_url)
# /channel/UCVrJcoR8hUN9Rn7uPI4z6NQ/about // 「https://www.youtube.com」が入らない
成功的方向(urlparse.urljoin)
首先安装模块
(以下命令是从$历史输出结果中提取的,可能不是全部必要的)
$ pip3 install urllib3
$ pip install git+https://github.com/mitsuhiko/flask-oauth
$ pip install urlparse3
汁料
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
channel_urls = ['/channel/UCVrJcoR8hUN9Rn7uPI4z6NQ']
for i in channel_urls:
root_url = 'https://www.youtube.com'
channel_url = ('%s' % i)
channel_about_url = urlparse.urljoin(youtube_url, channel_url, 'about')
print(self.open_channel_url)
# https://www.youtube.com/channel/UCVrJcoR8hUN9Rn7uPI4z6NQ/about
请参考以下链接:
https://qiita.com/Go-zen-chu/items/d7e6b9af0bd90c7aabca
https://www.it-swarm.dev/ja/python/python%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%ABurlparse%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%A7%E3%81%8D%E3%81%BE%E3%81%9B%E3%82%93/961197002/
https://github.com/heroku/kafka-helper/issues/6