Get a Python web page, character encode it, and display it

Premise

I think there are various methods, but it is the basic method in the basics.

Get WEB page

Use Requests from a third party library. You can also get it with the urllib.request module of the standard library. With the standard library, only basic GET and POST requests. Adding HTTP headers and basic authentication is troublesome. So I use the library "Requests".

import requests

# GET
r = requests.get(URL)
#GET parameter
r = requests.get(URL, params={'key': 'val'})

# POST
p = requests.post(URL, data={'key': 'val'})

#Basic authentication
b = requests.get(URL, auth=('User ID', 'password'))

Get and encode a web page

Estimate the encoding from the bytes in the response body

import sys
import requests

url = sys.argv[1]
r = requests.get(url)
#r.json()You can also get it with json

r.encoding = r.apparent_encoding
print(r.text)

Get the encoding from the meta tag

import sys
import re
import requests

url = sys.argv[1]
r = requests.get(url)

#Assuming the charset is at the beginning, only the beginning is decoded as an ASCII string
scanned_text = r.content[:1024].decode('ascii', errors='replace')

#Extract the charset value with a regular expression from the decoded string
match = re.search(r'charset=["\']?([\w-]+)', scanned_text)
if match:
    r.encoding = match.group(1)
else:
    r.encoding = 'utf-8'

print(r.text)

Recommended Posts

Get a Python web page, character encode it, and display it
Get an image from a web page and resize it
[Python3] Take a screenshot of a web page on the server and crop it further
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Get a capture of the entire web page in Selenium Python VBA
[Personal memo] Get data on the Web and make it a DataFrame
Create a web map using Python and GDAL
Launch a web server with Python and Flask
Let's write a Python program and run it
Extract data from a web page with Python
Try to get a web page and JSON file using Python's Requests library
Wagtail Recommendations (2) Let's define, edit, and display a page
Get mail from Gmail and label it with Python3
Get the matched string with a regular expression and reuse it when replacing on Python3
3-3, Python strings and character codes
Temporarily save a Python object and reuse it in another Python
Associate Python Enum with a function and make it Callable
Recursively get the Excel list in a specific folder with python and write it to Excel.
Get a global IP and export it to Google Spreadsheets
Web crawling, web scraping, character acquisition and image saving with python
Python learning basics ~ How to output (display) a character string? ~
[python] Get quotient and remainder
Display Google Maps on a web page (Spring Boot + Thymeleaf)
Display a web page with FastAPI + uvicorn + Nginx (SSL / HTTPS)
[python, ruby] fetch the contents of a web page with selenium-webdriver
Delete a particular character in Python if it is the last
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
I wrote a script to extract a web page link in Python
A popular web drinking party, Python detects silence and plays voice.
Python a + = b and a = a + b are different
Is it a character string operation?
Get web screen capture with python
Python2 str / unicode and encode / decode
[Python] return A [or / and] B
Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
How to input a character string in Python and output it as it is or in the opposite direction.
I want to pass an argument to a python function and execute it from PHP on a web server
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
Display a web page with FastAPI + uvicorn + Nginx (Templates function by Jinja2)
Deploy a Python app on Google App Engine and integrate it with GitHub
Until you get a snapshot of Amazon Elasticsearch service and restore it
Let's throw away JavaScript and write a web front end in Python!
A python program that resizes a video and turns it into an image
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Make a decision tree from 0 with Python and understand it (4. Data structure)
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
I want to make a web application using React and Python flask
Calculate and display standard weight with python
Display a list of alphabets in Python 3
Display matplotlib diagrams in a web application
I made a character counter with Python
A memo with Python2.7 and Python3 on CentOS
# 5 [python3] Extract characters from a character string
Connect a lot of Python or and and
[Python] How to invert a character string
Daemonize a Python web app with Supervisor
How to get a stacktrace in python
[Personal note] Web page scraping with python3
[Python] A quick web application with Bottle!
[python] Get a list of instance variables
Get a token for conoha in python