Requests (Python): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
(Nieuwe pagina aangemaakt met 'Python kent diverse bibliotheken om een webclient te emuleren. ''Requests'' lijkt de populaire te zijn.') |
|||
Regel 1: | Regel 1: | ||
− | Python kent diverse bibliotheken om een webclient te emuleren. ''Requests'' lijkt de | + | Python kent diverse bibliotheken om een webclient te emuleren. ''Requests'' lijkt de populairste te zijn (zomer 2019). |
+ | |||
+ | == Inloggen op een afgeschermde pagina == | ||
+ | |||
+ | Dat gaat met <code>requests</code> verbazend simpel. Sterker nog: Het voorbeeld op de [https://pypi.org/project/requests/ home page] demonstreet dit al: | ||
+ | |||
+ | <pre> | ||
+ | >>> r = requests.get('http://example.strompf.com', auth=('xxx','yyy')) | ||
+ | >>> r.status_code | ||
+ | 200 | ||
+ | >>> r.headers | ||
+ | {'Content-language': 'en', 'Expires': 'Thu, 01 Jan 1970 00:00:00 GMT', 'Vary': 'Accept-Encoding,Cookie', 'X-Powered-By': 'PHP/5.5.9-1ubuntu4.27', 'Date': 'Fri, 02 Aug 2019 12:14:12 GMT', 'Cache-Control': 'private, must-revalidate, max-age=0', 'Server': 'Apache/2.4.7 (Ubuntu)', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Encoding': 'gzip', 'Keep-Alive': 'timeout=5, max=49', 'Connection': 'Keep-Alive', 'Content-Length': '32237', 'Last-Modified': 'Fri, 02 Aug 2019 10:03:24 GMT'} | ||
+ | >>> r.headers['content-type'] | ||
+ | 'text/html; charset=UTF-8' | ||
+ | >>> r.encoding | ||
+ | 'UTF-8' | ||
+ | >>> r.text | ||
+ | '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html lang="en" dir="ltr">\n<head>\n<title>Main Page - Example</title>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n<meta name="generator" content="MediaWiki 1.16.4" />\n<link rel="alternate" type="application/x-wiki" title="Edit" href="/index.php?title=Main_Page&action=edit" />\n<link rel="edit" title="Edit" href="/index.php?title=Main_Page&action=edit" />\n<link rel="shortcut icon" href="/favicon.ico" />\n<link rel="search" type="application/opensearchdescription+xml" href="/opensearch_desc.php" title="Example (en)" />\n<link rel="alternate" type="a | ||
+ | </pre> | ||
+ | |||
+ | ''' Ter verificatie ''' | ||
+ | |||
+ | <pre> | ||
+ | #! /usr/bin/python3 | ||
+ | # | ||
+ | # Experiments with requests | ||
+ | ################################################################### | ||
+ | # | ||
+ | # | ||
+ | import requests | ||
+ | |||
+ | print(">>> Login zonder credentials...") | ||
+ | r = requests.get('http://example.strompf.com') | ||
+ | print(r.status_code) | ||
+ | |||
+ | print(">>> Login met correcte credentials...") | ||
+ | r = requests.get('http://example.strompf.com', auth=('xxx','correcte wachtwoord')) | ||
+ | print(r.status_code) | ||
+ | |||
+ | print(">>> Login met incorrecte credentials...") | ||
+ | r = requests.get('http://example.strompf.com', auth=('xxx','Verkeerde wachtwoord')) | ||
+ | print(r.status_code) | ||
+ | </pre> | ||
+ | |||
+ | Uitvoer: | ||
+ | |||
+ | <pre> | ||
+ | >>> Login zonder credentials... | ||
+ | 401 | ||
+ | >>> Login met correcte credentials... | ||
+ | 200 | ||
+ | >>> Login met incorrecte credentials... | ||
+ | 401 | ||
+ | </pre> | ||
+ | |||
+ | P.s.: Handjevol HTTP Status-codes: | ||
+ | |||
+ | * 200: OK | ||
+ | * 301: Moved Permanently | ||
+ | * 307: Temporary Redirected | ||
+ | * 400: Bad Request | ||
+ | * 401: Unauthorized | ||
+ | * 403: Forbidden - Ik geloof dat je dit krijgt als je Google Search probeert te ''scrapen'' | ||
+ | * 404: Not found | ||
+ | * 504: Gateway Timeout - Krijg ik regelmatig op https://couchsurfing.com |
Versie van 5 aug 2019 08:09
Python kent diverse bibliotheken om een webclient te emuleren. Requests lijkt de populairste te zijn (zomer 2019).
Inloggen op een afgeschermde pagina
Dat gaat met requests
verbazend simpel. Sterker nog: Het voorbeeld op de home page demonstreet dit al:
>>> r = requests.get('http://example.strompf.com', auth=('xxx','yyy')) >>> r.status_code 200 >>> r.headers {'Content-language': 'en', 'Expires': 'Thu, 01 Jan 1970 00:00:00 GMT', 'Vary': 'Accept-Encoding,Cookie', 'X-Powered-By': 'PHP/5.5.9-1ubuntu4.27', 'Date': 'Fri, 02 Aug 2019 12:14:12 GMT', 'Cache-Control': 'private, must-revalidate, max-age=0', 'Server': 'Apache/2.4.7 (Ubuntu)', 'Content-Type': 'text/html; charset=UTF-8', 'Content-Encoding': 'gzip', 'Keep-Alive': 'timeout=5, max=49', 'Connection': 'Keep-Alive', 'Content-Length': '32237', 'Last-Modified': 'Fri, 02 Aug 2019 10:03:24 GMT'} >>> r.headers['content-type'] 'text/html; charset=UTF-8' >>> r.encoding 'UTF-8' >>> r.text '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html lang="en" dir="ltr">\n<head>\n<title>Main Page - Example</title>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n<meta name="generator" content="MediaWiki 1.16.4" />\n<link rel="alternate" type="application/x-wiki" title="Edit" href="/index.php?title=Main_Page&action=edit" />\n<link rel="edit" title="Edit" href="/index.php?title=Main_Page&action=edit" />\n<link rel="shortcut icon" href="/favicon.ico" />\n<link rel="search" type="application/opensearchdescription+xml" href="/opensearch_desc.php" title="Example (en)" />\n<link rel="alternate" type="a
Ter verificatie
#! /usr/bin/python3 # # Experiments with requests ################################################################### # # import requests print(">>> Login zonder credentials...") r = requests.get('http://example.strompf.com') print(r.status_code) print(">>> Login met correcte credentials...") r = requests.get('http://example.strompf.com', auth=('xxx','correcte wachtwoord')) print(r.status_code) print(">>> Login met incorrecte credentials...") r = requests.get('http://example.strompf.com', auth=('xxx','Verkeerde wachtwoord')) print(r.status_code)
Uitvoer:
>>> Login zonder credentials... 401 >>> Login met correcte credentials... 200 >>> Login met incorrecte credentials... 401
P.s.: Handjevol HTTP Status-codes:
- 200: OK
- 301: Moved Permanently
- 307: Temporary Redirected
- 400: Bad Request
- 401: Unauthorized
- 403: Forbidden - Ik geloof dat je dit krijgt als je Google Search probeert te scrapen
- 404: Not found
- 504: Gateway Timeout - Krijg ik regelmatig op https://couchsurfing.com