[問題] 新手爬蟲請益

看板Python作者 (chawit)時間2年前 (2022/09/02 01:04), 2年前編輯推噓3(3014)
留言17則, 5人參與, 2年前最新討論串1/1
板上的各位大大們好,最近剛開始自學爬蟲,知道可以用find_all去抓所有同標籤和屬性 下的網頁文字內容,但想請問如果我想抓html裡面的特定內容,沒有顯示在網頁的那種, 如下圖的黃色miaowu2020的部分該如何下手呢?謝謝! https://i.imgur.com/NhgYWme.jpg
附上我抓其他網頁內容的程式 https://i.imgur.com/uuSK3Io.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.82.62.74 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1662051879.A.BCC.html

09/02 03:14, 2年前 , 1F
印象裡 find_all("a", {"data-item-id": "miaowu2020"})
09/02 03:14, 1F
剛試過好像不是,因為我是想找所以data-item-id後面的名字QQ

09/02 03:50, 2年前 , 2F
get("data-item-id")
09/02 03:50, 2F
謝謝J大,剛剛研究一下用get好像可以抓出來了,但多了很多顯示為none 的值就是... https://i.imgur.com/POoLHJe.jpg
https://i.imgur.com/5MxaSQ7.jpg
※ 編輯: etudiant (111.82.62.74 臺灣), 09/02/2022 13:24:58

09/02 14:37, 2年前 , 3F
你有看過t.attrs嗎
09/02 14:37, 3F

09/02 14:55, 2年前 , 4F
l大您好,我沒看過我來研究一下,謝謝您
09/02 14:55, 4F
※ 編輯: etudiant (111.82.62.74 臺灣), 09/02/2022 15:45:44

09/02 16:16, 2年前 , 5F
程式碼不要用截圖盡量用線上文本 貼圖很難Debug
09/02 16:16, 5F

09/02 16:28, 2年前 , 6F
謝謝S大提醒,我等到貼上來文字
09/02 16:28, 6F
import requests import bs4 import time import random URL="https://www.zeczec.com/categories?category=16&page=1&type=crowdfunding" headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKi t/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'} response = requests.get(URL,headers=headers) soup = bs4.BeautifulSoup(response.text,"html.parser") cash = soup.find_all('div','font-bold leading-8') for c in cash: print(c.text.strip()) for name in soup.find_all('a'): print(name.get('data-item-id')) delay_choices = [8, 5, 10, 6, 20, 11] delay = random.choice(delay_choices) time.sleep(delay) ※ 編輯: etudiant (111.82.62.74 臺灣), 09/02/2022 16:29:08

09/02 16:32, 2年前 , 7F
目前主要是想在這個網站外面總覽的部分先抓好每一個商品
09/02 16:32, 7F

09/02 16:32, 2年前 , 8F
的id,因為有發現它點進去個別商品的網頁名稱最後都是
09/02 16:32, 8F

09/02 16:32, 2年前 , 9F
用data-item-id結尾的,想說也許這樣一次爬下來就能設計
09/02 16:32, 9F

09/02 16:32, 2年前 , 10F
一個一口氣抓完個別內容的程式碼(?
09/02 16:32, 10F

09/02 16:47, 2年前 , 11F
可以嘗試一下if else來篩選非None的值吧
09/02 16:47, 11F

09/02 19:26, 2年前 , 12F
09/02 19:26, 12F

09/02 19:31, 2年前 , 13F
https://paste.ee/p/xOxLW 最後改迴圈
09/02 19:31, 13F

09/02 20:04, 2年前 , 14F
非常謝謝樓上s大,我馬上來研究一下!!
09/02 20:04, 14F

09/02 20:20, 2年前 , 15F
不懂再問^^
09/02 20:20, 15F

09/02 21:18, 2年前 , 16F
感謝好心s大,大致上都看懂了,除了attrs[]那邊的用法
09/02 21:18, 16F

09/02 21:18, 2年前 , 17F
我不太懂可能要花點時間補些資料看XD
09/02 21:18, 17F
文章代碼(AID): #1Z4EOdlC (Python)
文章代碼(AID): #1Z4EOdlC (Python)