programming language/Python

python ๊ธฐ์ดˆ์ •๋ฆฌ

moaoh 2021. 9. 5. 00:53

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๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํŽธ

 
๋Œ“๊ธ€์ˆ˜0