Use urlparse.urljoin instead of os.path.join for Python URL joins

I got a list of URLs from a csv file in Python and stumbled when I wanted to combine the URLs, so make a note

Those who did not work (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 "does not enter

Those who succeeded (urlparse.urljoin)

First install the module (The following commands are picked up from the output of $ histroy, so not all are needed)

tamenal.


$ pip3 install urllib3
$ pip install git+https://github.com/mitsuhiko/flask-oauth
$ pip install urlparse3

Source

sample.py


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

reference 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

Recommended Posts

Use urlparse.urljoin instead of os.path.join for Python URL joins
Use data class for data storage of Python 3.7 or higher
Let's use usercustomize.py instead of sitecustomize.py
Wrap (part of) the AtCoder Library in Cython for use in Python
Let's use tomotopy instead of gensim
Proper use of Python visualization packages
Next, use Python (Flask) for Heroku!
[Python Queue] Convenient use of Deque
Survey for practical use of BlockChain
Beginners use Python for web scraping (1)
Introductory table of contents for python3
Beginners use Python for web scraping (4) ―― 1
Record of Python introduction for newcomers
[Python of Hikari-] Chapter 05-09 Control syntax (use of for statement and while statement properly)
About Python code for simple moving average assuming the use of Numba
Effective Python memo Item 7 Use list comprehension instead of map and filter
Use edump instead of var_dump for easy debugging & efficient data content (PHP)
Why you should use urlopen instead of urlretrieve to download Python files
[Python] Summary of how to use pandas
[Python] Minutes of study meeting for beginners (7/15)
Use DeepL with python (for dissertation translation)
Summary of various for statements in Python
python string comparison / use'list'and'in' instead of'==' and'or'
[Python] Organizing how to use for statements
[Python2.7] Summary of how to use unittest
R: Use Japanese instead of Japanese in scripts
Pandas of the beginner, by the beginner, for the beginner [Python]
Summary of useful techniques for Python Scrapy
How to use "deque" for Python data
Summary of how to use Python list
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
[Python2.7] Summary of how to use subprocess
[Question] How to use plot_surface of python
[Python] User Warning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
The story of low learning costs for Python
[Python] How to use two types of type ()
Use logger with Python for the time being
Use multiple versions of python environment with pyenv
Wrap C with Cython for use from Python
Use OpenSeesPy regardless of OS or Python version
Summary of how to use MNIST in Python
Image processing? The story of starting Python for
Easy understanding of Python for & arrays (for super beginners)
python development environment -use of pyenv and virtualenv-
Don't use readlines () in your Python for statement!
Summary of frequently used Python arrays (for myself)
Code for checking the operation of Python Matplotlib
Summary of studying Python to use AWS Lambda
Let's use different versions of SQLite3 from Python3!
Wrap C ++ with Cython for use from Python
[Python for Hikari] Chapter 09-01 Classes (Basics of Objects)
python: Use your own class for numpy ndarray
Basic story of inheritance in Python (for beginners)
What to use for Python stacks and queues (speed comparison of each data structure)