When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo

background

As the title says. Even if I googled quite a bit, I could not find a solution in Japanese or English, so make a note so that I can refer to it in case of recurrence.

Code in which the error occurred

It's not actually wikipedia, but it happened when I scraped the https: // ~ site. If you are touching the PC now, you can scrape the site without any error. Why ... environmental problems?

import requests

url = 'https://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8'
response = requests.get(url)

result = response.text
print(result)

The error message at that time is not reserved, but I remember that the word bad handshake was included in SSLError.

approach

I didn't want to use the verify = False strategy, so I did a lot of research and found that using ʻurllib and ssl` was able to scrape without error. Once you get over here, all you have to do is extract the elements you want to use with Beautiful Soup 4.

import urllib.request
import ssl

url = 'https://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8'
context = ssl.SSLContext()
req = urllib.request.Request(url=url)
with urllib.request.urlopen(req, context=context) as f:
    result = f.read().decode()
print(result)

Referenced site

Recommended Posts

When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
What I was addicted to when using Python tornado
I tried to make a stopwatch using tkinter in python
I tried using pipenv, so a memo
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
A story I was addicted to when inserting from Python to a PostgreSQL table
[Memo] I tried a pivot table in Python
A story that didn't work when I tried to log in with the Python requests module
[Python] When I tried to make a decompression tool with a zip file I just knew, I was addicted to sys.exit ()
What I was addicted to when creating a web application in a windows environment
Three things I was addicted to when using Python and MySQL with Docker
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I tried to implement a pseudo pachislot in Python
I was addicted to scraping with Selenium (+ Python) in 2020
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
A story that was convenient when I tried using the python ip address module
I tried to implement a one-dimensional cellular automaton in Python
A memo when creating a directed graph using Graphviz in Python
I tried "How to get a method decorated in Python"
What I was addicted to when migrating Processing users to Python
I stumbled when I tried to install Basemap, so a memorandum
What I was addicted to when dealing with huge files in a Linux 32bit environment
I tried to automate "one heart even if separated" using a genetic algorithm in Python
The story I was addicted to when I specified nil as a function argument in Go
I tried to make a regular expression of "amount" using Python
What I was addicted to when introducing ALE to Vim for Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
What I was addicted to with json.dumps in Python base64 encoding
[Python] A memo that I tried to get started with asyncio
A note I was addicted to when making a beep on Linux
I tried to make a regular expression of "date" using Python
I tried to implement a misunderstood prisoner's dilemma game in Python
A note I was addicted to when creating a table with SQLAlchemy
I get a can't set attribute when using @property in python
I tried to make a todo application using bottle with python
I was addicted to confusing class variables and instance variables in Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
Log in to Slack using requests in Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
What I was addicted to Python autorun
When I tried to run Python, it was skipped to the Microsoft Store
I was addicted to creating a Python venv environment with VS Code
I tried to develop a Formatter that outputs Python logs in JSON
The file name was bad in Python and I was addicted to import
I tried to create a sample to access Salesforce using Python and Bottle
I tried to implement a card game of playing cards in Python
I was addicted to trying Cython with PyCharm, so make a note
[Fabric] I was addicted to using boolean as an argument, so make a note of the countermeasures.
A memo that I wrote a quicksort in Python
I was able to recurse in Python: lambda
I want to create a window in Python
I tried playing a typing game in Python
I tried to implement TOPIC MODEL in Python