---
# System prepended metadata

title: PYTHON 簡單爬蟲

---

# PYTHON 簡單爬蟲
## 安裝套件
```
pip install beautifulsoup4
pip install requests
```
## 簡單爬取文華網頁
```python=
import requests
from bs4 import BeautifulSoup
response = requests.get("https://whsh.tc.edu.tw/")
soup = BeautifulSoup(response.text, "html.parser")
print(soup.prettify())  #輸出排版後的HTML內容
```
## ctf
```python=
import requests
from bs4 import BeautifulSoup

tags = 1
find = ""
#while 1 :
result = ""

while 1 :

    url = "http://192.168.0.42:4444/" + str(tags)
    print(url)
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    result = soup.find("h2")
    if result == None :
        tags +=1 
    else :
        print(result)
        break
```
## tryif
```python=
try :
    程式碼放置區
except :
    如果報錯就跳到這邊
```