實際跑了一下 code, 結果.html
看起來是有的,只是沒 print() 出結果。
程式碼有小改一下,以下是會動的 code 。
建議你可以先從了解程式碼開始!
import requests
from bs4 import BeautifulSoup
url = 'https://www.ptt.cc/bbs/NBA/index.html'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebkit/537.36'}
response = requests.get(url, headers=headers)
with open('結果.html', 'w', encoding='utf-8') as f:
f.write(response.text)
soup = BeautifulSoup(response.text, 'html.parser')
articles = soup.find_all('div', class_='r-ent')
for a in articles:
title = a.find('div', class_='title')
print(title.a.text)