施勢帆老師的教學網站

 找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 20|回復: 0

網路爬蟲

[複製鏈接]

817

主題

829

帖子

4111

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
4111
發表於 昨天 11:36 | 顯示全部樓層 |閱讀模式
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  3 11:26:32 2026

@author: AEUST
"""

import requests
from bs4 import BeautifulSoup

url = 'https://www.ptt.cc/'
web = requests.get('https://www.ptt.cc/bbs/AllTogether/index.html', cookies={'over18':'1'})
soup = BeautifulSoup(web.text, "html.parser")
titles = soup.find_all('div', class_='title')     # 取得 class 為 title 的 div 內容
for i in titles:
    if i.find('a') != None:                         # 判斷如果不為 None
        print(i.find('a').get_text())                 # 取得 div 裡 a 的內容,使用 get_text() 取得文字
        print(url + i.find('a')['href'], end='\n\n')  # 使用 ['href'] 取得 href 的屬性

print("123456789  施勢帆")        

回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|施勢帆老師的教學網站

GMT+8, 2026-3-4 22:47 , Processed in 0.061782 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表