python ๊ธฐ์ด์ ๋ฆฌ

python์ผ๋ก URL์์ ์๋ฃ ์ถ์ถํ๊ธฐ
- python ๊ธฐ๋ณธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ urllib : ์ฌ์ฉx, ๋ ๊ฐ๋ ฅํ ์จ๋ผ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉํ๊ธฐ ์ํด
- Request ์ฌ์ฉo
HTML์์ ์ ๋ณด ์ถ์ถํ๊ธฐ
- Beautiful Soup4
[Python3 / Mac ] ์น ํฌ๋กค๋ง์ ์ํ ์ค๋น - pip, requests, beautifulsoup4 ์ค์น
.string .text ์ฐจ์ด์
.string ๊ณผ .text ์ฐจ์ด๊ฐ ๋ฌด์์ธ๊ฐ์ - ์ธํ๋ฐ | ์ง๋ฌธ & ๋ต๋ณ
.string ํ๊ทธ ํ์์ ๋ฌธ์์ด์ ๊ฐ์ฒดํํฉ๋๋ค. ๋ฌธ์์ด์ด ์์ผ๋ฉด None์ ๋ฐํํฉ๋๋ค.
.text๋ ํ์ ์์ ํ๊ทธ์ ํ ์คํธ๊น์ง ๋ฌธ์์ด๋ก ๋ฐํํฉ๋๋ค. (์ ๋์ฝ๋ ํ์)
๋ธ๋ผ์ฐ์ ์๋ต ๊ฐ๊ณผ ํฌ๋กค๋ง ๊ฐ์ด ๋ค๋ฅธ ์ด์
ํฌ๋กค๋ง์์๋ User-Agent๊ฐ์ ์๊ดํ์ง ์์ง๋ง ์ฐ๋ฆฌ๊ฐ ์นํ์ด์ง๋ฅผ ๋ค์ด๊ฐ ๊ฒฝ์ฐ์๋ User-Agent๊ฐ์ด ๊ด์ฌํด์
๋ค๋ฅด๊ฒ ์นํ์ด์ง๋ฅผ ๋ด๋ณด๋ด๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๊ฐ ํฌ๋กค๋ง์ ํ์ฌ ๋ณด์ด๋ ๊ฐ๊ณผ ์นํ์ด์ง๋ก ๋ค์ด๊ฐ์๋ ๋ณด์ฌ์ง๋ ๊ฐ์
์ฐจ์ด๊ฐ ๋ฐ์๋ ์ ์๋ค.
๊ทธ๋ฌ๋ฏ๋ก ํฌ๋กค๋ง์ ํ ๋ User-Agent๊ฐ์ ๋ฃ์ด์ฃผ๊ฒ ๋๋ฉด ๋๊ฐ์ด ๋์์ด ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
import requests
from bs4 import BeautifulSoup
# URL = f"https://stackoverflow.com/jobs?q=python"
# headers = {"User-Agent" : ์์ ์ Agent(์ด์์ฒด์ ์ ๋ธ๋ผ์ฐ์ )์ ๋ํ ์ ๋ณด}
result = requests.get(URL,headers=headers)
User-Agent ๊ฐ ํ์ธํ๊ธฐ
What is my user agent?
Every request your web browser makes includes your User Agent; find out what your browser is sending and what this identifies your system as.
www.whatismybrowser.com
args *kwargs
def plus(a, b, *args, **kwargs):
print(args)
print(kwargs)
return a + b
plus(1, 2, 1, 1, 1, 1, 1, 1, hello=True, my=True, name=True)
*args : ๋ณ์๋ฅผ ๋ฌด์ ํ์ผ๋ก ๋ฐ์ ์ ์๊ฒ ๋ง๋ฆ
**kwargs : ํค์๋๋ก ์ง์ ๋ ๋ณ์๋ฅผ ๋ฌด์ ํ์ผ๋ก ๋ฐ์ ์ ์๊ฒ ๋ง๋ฆ
class
class Car():
windows = 4
porche = Car()
print(porche.windows)
>> 4
๊ธฐ๋ณธ ํ์
blueprint ์ค๊ณ๋
๊ตฌ์กฐ์ฒด์ ๊ฐ์ด ๋ด์ฉ๋ฌผ๋ค์ ๋ด์์ ์ ์ฅํ ์ ์๋ค.
class Car():
windows = 4
def start(self):
print("start")
def hello():
print("hello world")
class ์์ ์ ์ธ๋ ํจ์๋ฅผ method๋ผ๊ณ ๋ถ๋ฅธ๋ค.
hello์ ๊ฒฝ์ฐ์๋ function
๋ชจ๋ method๋ ์๊ธฐ ์์ ์ ์ฒซ ๋ฒ์งธ argument๋ก ๊ฐ์ง๊ธฐ ๋๋ฌธ์ self๋ก ๋ฃ์ด์ฃผ๋ ๊ฒ์ด ์ข๋ค.
class Car():
windows = 4
print(dir(Car))
>> ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'windows']
dir() : class์์ ์๋ ๋ชจ๋ ๊ฒ๋ค์ ๋ฆฌ์คํธ๋ก ๋ณด์ฌ์ค๋ค.
str
ํด๋น ํด๋์ค๊ฐ ํธ์ถ๋ ๊ฒฝ์ฐ instance๋ฅผ ์ถ๋ ฅํ๋ค.
class Car():
windows = 4
porche = Car()
print(porche)
>> <__main__.Car object at 0x1085dcf70>
# =================================================
class Car():
windows = 4
def __str__(self):
return "hello"
porche = Car()
print(porche)
>> hello
class ๋ด์ ์ด๋ฏธ ์กด์ฌํ๋ method๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ ์ ํด์ฃผ๋ ๋ฐฉ์
init
์์ฑ์
class Car():
def __init__(self, **kwargs):
self.windows = 4
self.color = kwargs.get("color", "black")
self.price = kwargs.get("price", "$20")
def __str__(self):
return "hello"
porche = Car(color="green", price="$40")
print(porche.color, porche.price)
mini = Car()
print(mini.color, mini.price)
>>green $40
black $20
์ธ์คํด์ค๋ฅผ ๋ง๋ค ๋ ํธ์ถ๋๋ ํน๋ณํ ๋ฉ์๋
์์
class Car():
def __init__(self, **kwargs):
self.windows = 4
self.color = kwargs.get("color", "black")
self.price = kwargs.get("price", "$20")
def __str__(self):
return "hello"
class convertible(Car):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.time = kwargs.get("time", 10)
def take_off(self, **kwargs):
return "taking off"
def __str__(self):
return "Car with no roof"
porche = convertible(color="green", price="$40")
print(porche)
print(porche.color)
>> Car with no roof
green
super() : ๋ถ๋ชจ ํด๋์ค๋ฅผ ํธ์ถํ๋ ํจ์
super().init(**kwargs) ๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ์ ์ง ์์๋ค๋ฉด init์ด ์ฌ์ ์ธ์ด ๋์ด
time ์ด๋ผ๋ ๊ฐ๋ง ๋จ๊ฒ ๋์ง๋ง super๋ฅผ ํธ์ถํ์ฌ ๋ถ๋ชจ ํด๋์ค๋ฅผ ๊ฐ์ ธ์ด์ผ๋ก์จ ๋ถ๋ชจ์ ์๋ ๊ฐ๊ณผ
์์์ ์๋ ๊ฐ ๋ชจ๋ ์ฌ์ฉ ๊ฐ๋ฅ
flask
from flask import Flask
app = Flask("SuperScrapper")
@app.route("/")
def home():
return ("HELLO! Welcome")
@app.route("/contact")
def contact():
return "Contact me!"
app.run(host="0.0.0.0")
@app.route() ์ ๊ฒฝ์ฐ์๋ ๋ฐ๋ก ๋ฐ์ ์๋ ํจ์๋ฅผ ํธ์ถํ๋ ๋ฐฉ์
@app.route("/<username>")
def contact(username):
return f"Contact {username} me!"
์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ ์ํ ๊ฒฝ์ฐ URL/{username} ํ์์ผ๋ก ๋ค์ด๊ฐ์ ์ด๋ ํ ๊ฐ์ด ๋ค์ด์๋
Contact {username} me! ์ ๋ง์ถฐ ๋ฐํ์ด ๋๋ค
render_template
from flask import Flask, render_template
app = Flask("SuperScrapper")
@app.route("/")
def home():
return render_template("potato.html")
- main.py
- templates
- potato.html
render_template : html ํ์ผ์ ๋ถ๋ฌ์์ ์ ์ฉ์ํจ๋ค
templates์์ ์๋ ํ์ผ์ ์ฐพ์์ ์ ์ฉ์ํค๋ ๋ฐฉ์
( templates ์ง์ ๋ ๋๋ ํฐ๋ฆฌ html ์์น๋ฅผ ๋ณ๊ฒฝํ๊ฑฐ๋ ๋๋ ํ ๋ฆฌ ๋ช
์ด ๋ค๋ฅธ ๊ฒฝ์ฐ ์คํ์ด ๋์ง ์์ )
request
from flask import Flask, render_template, request
@app.route("/report")
def report():
word = request.args.get("word")
return f"your word is {word}"
input์ผ๋ก ๋ฐ์ argument ์ ๋ณด๋ฅผ ๋ฝ์์ฌ ์ ์๋ค.
@app.route("/report")
def report():
word = request.args.get("word")
return render_template("report.html", SearchingBy=word)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Job Search</title>
</head>
<body>
<h1>Search Results</h1>
<h3>You are looking for {{SearchingBy}}</h3>
</body>
</html>
์ด๋ฌํ ํ์์ผ๋ก ๋ฐ์ ๊ฐ์ html์ ๋๊ฒจ์ค ์ ์๋ค
flask vs Django
flask์ ๊ฒฝ์ฐ ์ ์ ์ฝ๋๋ฅผ ์ ์ด์ผ ํ ๊ฒฝ์ฐ์๋ ๋์์ง ์์ง๋ง
์ค์ ๋ก db๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ์๋ Django๋ณด๋ค flask๊ฐ ๋ ๋ณต์กํ๊ณ ์ด๋ ค์ด ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
๊ทธ๋์ ์ค์ ๋ก๋ flask๋ฅผ ์ ์ฌ์ฉํ์ง ์๊ณ Django๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ ํธ