Re: [問題] 爬蟲如何選取ptt內文內容

看板Python作者 (談無慾)時間7年前 (2017/01/13 23:33), 7年前編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《ahahahahah (あああああ)》之銘言: : 嫩嫩爬蟲新手 : 請問一下各位大大 : 爬蟲ptt如何抓下內文,我只想要爬內文就好,不要推文..... : http://i.imgur.com/BeEIMBc.jpg
: (不好意思借用一下隔壁軟體板) : 我用chrome檢查工具 : 發現內文包含在id="main-content"裡面 : 更下面的tag有作者、標題、推文等.... : 但是似乎沒有單獨內文的tag : 我用suop.select('#main-content')[0].text : 但是抓下的是包含作者標題推文等一大串內容.....囧 : 請問要如何處理這個問題? : 謝謝~ http://imgur.com/a/YBwYF 要在這底下找 才會有東西 自己參考一下CODE吧 不過這2015寫的 不知道後面有沒有改過 自己參考一下吧 之前的PTT GS版的CODE res = requests.get('https://www.ptt.cc/bbs/Gamesale/M.1437629857.A.0DD.html') soup = BeautifulSoup(res.text,"html.parser") f = open("D:/Ptt_data/Gamesale_word.csv","w") w = csv.writer(f) w.writerow([u'作者', u'日期', u'標題', u'價格']) main_content = soup.find(id="main-content") metas = main_content.select('div.article-metaline') #print(metas) #這邊是印出文章內頁的文章名稱跟一些資訊 目前不需要 filtered = [ v for v in main_content.stripped_strings if v[0] not in [u'※', u'◆'] and v[:2] not in [u'--'] ] #filtered = [_f for _f in filtered if _f] content = ' '.join(filtered) content = re.sub(r'(\s)+', '', content ) #print(content) number_start = content.index(u'價') number_end = content.index(u'地') author = metas[0].select('span.article-meta-value')[0].string title = metas[1].select('span.article-meta-value')[0].string date = metas[2].select('span.article-meta-value')[0].string price = content[number_start+3 : number_end-1] data = [ [author, date, title, price]] #這裡要注意一下存的格() [] w.writerows(data) f.close() print() print("It's done.") print() -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.248.151.246 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1484321622.A.951.html ※ 編輯: MOONY135 (111.248.151.246), 01/13/2017 23:37:24

01/13 23:43, , 1F
謝謝你!我研究一下
01/13 23:43, 1F

01/14 04:39, , 2F
01/14 04:39, 2F
文章代碼(AID): #1OUFDMbH (Python)
文章代碼(AID): #1OUFDMbH (Python)