No. | Content. | Remark. |
---|---|---|
1 | Html Code Change | ์ ํ๋ธ ์ธ๋ค์ผ ๋ฐ๊พธ๊ธฐ |
2 | GGG_world | url ๊ฒ์ฌ |
3 | Wikipedia : ISO 3166-1 | ๊ตญ๊ฐ ๋์ด ๋ฐ ๊ฒ์ / url ์ ๊ณต |
์น ์์ HTML์ ์ฝ์ด์ ํน์ ๋ฐ์ดํฐ๋ฅผ ์์งํ๋ ํ์
- ๋ฐ์ดํฐ ๊ฐ์ฒด ์ ์ + ์น ํ์ด์ง ์ ์
- ์น ํ์ด์ง์์ ์ถ์ถํ ๋ฐ์ดํฐ ๋ถ์
- ์น์คํฌ๋ํ ์ฝ๋ ์์ฑ
- ์ถ์ถํ ๋ฐ์ดํฐ ์ ์ฅ
Requests๋ HTTP ์์ฒญ์ ๋ณด๋ด๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก HTTP GET, POST, PUT, DELETE ๋ฑ์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
๋ํ dictionary๋ก ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ํ์ํ request ์ธ์ฝ๋ฉ์ ์๋์ผ๋ก ์ฒ๋ฆฌํด์ค๋ค.
import requests
url = "https://github.com/"
request = requests.get(url) # GET
print(request.text) # HTML ์ถ์ถ
url = "https://github.com/post"
dic={'kind':'zest', 'title':'Truffle', 'age':3}
request = requests.post(url, data=dic) # POST
print(request.text)
https://github.com/psf/requests
https://requests.readthedocs.io/projects/3/
BeautifulSoup4์ ์น ํ์ด์ง ์ ๋ณด๋ฅผ ์คํฌ๋ฉํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก HTML๊ณผ XML ๋ฌธ์ ๋ฑ์ ๋ถ์ํ๋ค.
from bs4 import BeautifulSoup
url = "https://github.com/"
request = requests.get(url)
soup = BeautifulSoup(request.text, 'html.parser')
https://www.crummy.com/software/BeautifulSoup/bs4/doc/
BeautifulSoup ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ง์ผ๋ก ๋ค์ํ ์ฌ์ดํธ์ ์ ๋ณด๋ฅผ ์ถ์ถํ ์ ์์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋์ ์ผ๋ก ์์ฑ๋ ์ ๋ณด๋ ๊ฐ์ ธ์ฌ ์ ์๋ค๋ ํ๊ณ๊ฐ ์๋ค.
๋ง์ฝ ์คํฌ๋ํ์ ์๋ํ๋ค๊ฐ ์๋ฌด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค์ง ๋ชปํ๋ค๋ฉด ๋๋ถ๋ถ์ ๊ฒฝ์ฐ๊ฐ ์๋ฐ์คํฌ๋ฆฝํธ๋ก html์ ๋ง๋ค์ด์ ๊ทธ๋ ๋ค.
๋ฐ๋ผ์ Selenium ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋์ ์ผ๋ก ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ํฌ๋กค๋ง ํ๊ธฐ ์ํด
- ์ฌ์ดํธ์ ๋ค์ํ html ์์์ ํด๋ฆญ, ํค๋ณด๋ ์ ๋ ฅ ๋ฑ ์ด๋ฒคํธ๋ฅผ ์ฃผ๊ธฐ ์ํด
- ๋ฐ๋ณต์ ์ผ๋ก ํ๋ ์น์์ ์ ๋ฌด ์๋ํ (ex. ์๋๋ก๊ทธ์ธ, ๋ธ๋ก๊ทธ ์ด์์๊ธ ์๋์ข์์์ ๋๊ธ ์์ฑ ๋ฑ)
from selenium import webdriver
url="http://google.com"
# driver ๊ฒฝ๋กํซ ํ์ผ๊ฒฝ๋ก์ ๊ฐ์ ๊ณณ์ ๋ ๊ฒฝ์ฐ
driver_same=webdriver.Chrome()
driver_same.get(url)
# driver ๊ฒฝ๋ก๋ฅผ ํ์ผ๊ฒฝ๋ก์ ๋ค๋ฅธ ๊ณณ์ ๋ ๊ฒฝ์ฐ
driver_diff=webdriver.Chrome("driver_diff์ ๊ฒฝ๋ก")
driver_diff.get(url)
ํฌ๋กฌ๋๋ผ์ด๋ฒ ์ค์น : https://chromedriver.chromium.org/downloads
Github ๋ธ๋ก๊ทธ ์ฒซ ํ์ด์ง์ ๋ ์๋ ํฌ์คํ url ์คํฌ๋กค๋ง ํ๊ธฐ
- url ์ถ์ถ์ ์ํด ์ ์ ํ ์น ์ฌ์ดํธ๋ฅผ ์ผ๋ ์ํ์์
F12
๋ฅผ ๋๋ฌ html ์ฝ๋์ฐฝ์ ํจ๋ค. ctrl + shift + c
๋ฅผ ๋๋ฅธ ์ํ์์ ์ํ๋ ๊ณณ์ ํด๋ฆญํ๋ฉด ํด๋น ์์น์ ๋งค์นญ ๋๋ html ์ฝ๋๋ฅผ ๋ณผ ์ ์๋ค.- ์น ์ฌ์ดํธ์์ ์ฝ๋์ฐฝ์ผ๋ก ๋์ด์ ํด๋น hmtl ์ฝ๋๋ฅผ ๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ผ๋ก ํด๋ฆญํ๋ฉด ๋จ๋ ์ฐฝ ์ค
Copy
>copy selector
๋ฅผ ์ ํํ๋ฉด ์ํ๋ ์ ๋ณด์ hmtl ์์น๊ฒฝ๋ก๋ฅผ ๋ณต์ฌํ๊ฒ ๋๋ค. - find, find_all ํน์ select ํจ์ ๋ฑ๊ณผ html ์์น๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ์ฌ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ค.
html์ฝ๋์ฐฝ ์์
์ฝ๋
import requests
from bs4 import BeautifulSoup
url = "https://github.blog/"
request = requests.get(url)
soup = BeautifulSoup(request.text, "html.parser")
results = soup.select("#main > section > div > div > div")
count=1
for result in results:
https=result.find("a")
link=https.attrs['href']
print(f"# {count} : ",link)
count+=1
๊ฒฐ๊ณผ
# 1 : https://github.blog/2021-08-11-githubs-engineering-team-moved-codespaces/
# 2 : https://github.blog/2021-08-11-githubs-engineering-team-moved-codespaces/
# 3 : https://github.blog/2021-08-16-securing-your-github-account-two-factor-authentication/
# 4 : https://github.blog/2021-08-12-teaching-learning-github-classroom-visual-studio-code/
# 5 : https://github.blog/2021-08-12-whats-new-from-github-changelog-july-2021-recap/
# 6 : https://github.blog/category/community/
# 7 : https://github.blog/category/education/
# 8 : https://github.blog/category/engineering/
# 9 : https://github.blog/category/enterprise/
# 10 : https://github.blog/category/open-source/
# 11 : https://github.blog/category/policy/
# 12 : https://github.blog/category/product/
# 13 : https://github.blog/category/security/