py 網路連線設定、公開資料串接

import urllib.request

# 下載特定網址資料
import urllib.request as request 
with request.urlopen(網址) as reponse:
	data=reponse.read()
print(data)

# 撈取台灣大學網頁
import urllib.request as request 
scr = "<https://www.ntc.edu.tw>"
with request.urlopen(scr) as response:
	data=response.read().decode("utf-8") # decode進行解碼
print(data)

# 練習串接公開資訊
import urllib.request as request 
import json
scr = "<https://data.taipei/api/v1/dataset/babda6a7-1d9c-4e2d-a726-dbe73a865352?scope=resourceAquire>"
with request.urlopen(scr) as response:
	# data=response.read().decode("utf-8")
	data=json.load(response) # 利用josn模組處理json資料格式
print(data)

web crawler 網路爬蟲

# 網路爬蟲
1.連線到特定網址,抓取資料
2.解析資料,取得實際想要的部分

# HTML格式資料(使用第三方套件BeautifulSoup 來做解析
# 使用PIP套件管理工具
# pip install beautifulsoup4
# 引入套件
from bs4 from beautifulSoup
# 使用bs4解析,第一參數是要解析的內容,第二參數為指定的解析氣
soup = beautifulSoup("","")

#使用內建方法自動排版
soup.prettify()

# replace 取代
# strip 消除空白與換行

# 如網頁標籤是隨機亂碼(加密)產生,使用css selector做篩選
soup.select('h1[class*="heroInfo__title__"]')
# 利用 Regex (正規表達式) 做篩選
import re
reg = re.compile('HeroInfo__title__')
soup.find('h1', class_=reg)

# 取回價格字串轉為純文字(使用decimal套件)
from re import sub
from decimal import Decimal
money = d.text
float(Decimal(sub(r'[^\\d.]', '', money)))

Untitled

# 執行會遇到403:Forbidden主要是網站不允許此爬蟲
# 透過開發者工具模仿使用者行為
# 建立Headers的資訊就可以成功爬取網頁內容

Untitled

#引入套件抓取標籤

Untitled

#抓取 PTT 電影版網頁原始碼(HTML)
import urllib.request as req
url="<https://www.ptt.cc/bbs/movie/index.html>"
# 建立一個 Request 物件,附加 Request Headers 的資訊
request=req.Request(url, headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
})
with req.urlopen(request) as response:
    data=response.read().decode("utf-8")

# 解析原始碼,取得每篇文章的標題
import bs4
root=bs4.BeautifulSoup(data, "html.parser")
# titles=root.find("div", class_="title") #尋找 class="title" 的 div 標籤(單一)
titles=root.find_all("div", class_="title") #尋找 class="title" 的 div 標籤(全部)
for title in titles:
    if title.a != None: # 如果標題包含 a 標籤 (沒有被刪除),印出來
        print(title.a.string)

web crawler - Cookie

#Cookie
1.連線到特定網站,抓取資料
2.解析資料,取得實際想要的部分

#什麼是Cookie?
網站存在在瀏覽器的一小段內容

#與伺服器的互動
連線時,放在Request Headers 中送出

#抓取 PTT 八卦版網頁原始碼(HTML)
import urllib.request as req
url="<https://www.ptt.cc/bbs/Gossiping/index.html>"
# 建立一個 Request 物件,附加 Request Headers 的資訊
request=req.Request(url, headers={
    "cookie": "over18=1",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
})
with req.urlopen(request) as response:
    data=response.read().decode("utf-8")

# 解析原始碼,取得每篇文章的標題
import bs4
root=bs4.BeautifulSoup(data, "html.parser")
# titles=root.find("div", class_="title") #尋找 class="title" 的 div 標籤(單一)
titles=root.find_all("div", class_="title") #尋找 class="title" 的 div 標籤(全部)
for title in titles:
    if title.a != None: # 如果標題包含 a 標籤 (沒有被刪除),印出來
        print(title.a.string)

# 抓取多頁面並包裝成函式

#抓取 PTT 八卦版網頁原始碼(HTML)
import urllib.request as req

def getData(url):
    # 建立一個 Request 物件,附加 Request Headers 的資訊
    request=req.Request(url, headers={
        "cookie": "over18=1",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
    })
    with req.urlopen(request) as response:
        data=response.read().decode("utf-8")

    # 解析原始碼,取得每篇文章的標題
    import bs4
    root=bs4.BeautifulSoup(data, "html.parser")
    # titles=root.find("div", class_="title") #尋找 class="title" 的 div 標籤(單一)
    titles=root.find_all("div", class_="title") #尋找 class="title" 的 div 標籤(全部)
    for title in titles:
        if title.a != None: # 如果標題包含 a 標籤 (沒有被刪除),印出來
            print(title.a.string)
    # 抓取上一頁的連結
    nextLink=root.find("a", string="‹ 上頁") # 找到內文是‹ 上頁的 a 標籤
    return nextLink["href"]

# 抓取一個頁面的標題
pageURL= "<https://www.ptt.cc/bbs/Gossiping/index.html>"
count=0
while count<3:
    pageURL= "<https://www.ptt.cc>"+getData(pageURL)
    count+=1

web crawler -AJAX