二手产品经理

二手产品经理

THIS IS RENO

爬蟲 - 95~96天 - 在線學python100天

記錄#

  1. 95 天是從 news 獲取 5 條新聞,提交給 openai 生成關鍵詞,再把關鍵詞送到 spotipy 返回歌曲。因為 openai 的關係,今天的練習跳過。
  2. 96 天是學習獲取 html 內容並解析,可能終於學到 python 最強大的功能了:爬蟲!
  3. 使用 response = requests.get(url) html = response.text 獲取網頁的 html 內容。
  4. 使用 soup = BeautifulSoup(html, 'html.parser') 對 html 格式化。在這之前要導入庫:from bs4 import BeautifulSoup
  5. 使用 soup.find_all("span", {"class", "titleline"}) 獲取指定內容。span 是標籤名,後面是類和類名。
  6. 今天的練習是:從 hacker news 獲取內容標題,如果含有 python 和 replit 則 print。過程中發現沒有包含這兩個關鍵詞的標題,所以增加了另一個關鍵詞:SQL

CODE#

main.py#

from bs4 import BeautifulSoup
import requests

url = "https://news.ycombinator.com"

respone = requests.get(url)
html = respone.text

soup = BeautifulSoup(html, 'html.parser')
title = soup.find_all("span", {"class", "titleline"})
print(len(title))

for txt in title:
  if "python" in txt.text or "replit" in txt.text or "SQL" in txt.text:
    print(txt.text)



載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。