作者s1021412107 (阿宗)
看板Python
標題[問題] 抓取留言
時間Mon Aug 27 13:33:54 2018
請問各位大大
小弟剛學爬蟲
想要抓取ptt裡的留言
但是我看網路只能抓留言的內容
推文、ID名稱、內容、時間怎麼同時抓下來
想要直接分類好推文還是噓文
Code如下:
import requests
from bs4 import BeautifulSoup
#所要擷取的網站網址
url = '
https://www.ptt.cc/bbs/movie/M.1535264750.A.3E3.html'
#建立回應
response = requests.get(url)
#印出網站原始碼
#print(response.text)
#將原始碼做整理
soup = BeautifulSoup(response.text, 'lxml')
#使用find_all()找尋特定目標
articles = soup.find_all('div', 'push')
#寫入檔案中
with open('movie_message.txt','w') as f:
for article in articles:
#去除掉冒號和左右的空白
messages = article.find('span','f3 push-content').getText().replace(':
','').strip()
print(messages)
f.write(messages + "\n")
https://i.imgur.com/dcD1cdz.jpg
https://i.imgur.com/qOreC31.jpg
https://i.imgur.com/cv3ixcj.jpg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 110.26.37.2
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1535348036.A.483.html
→ InfinityGate: 把div.push裡面的span都抓下來啊 08/27 16:38
→ InfinityGate: 爬蟲先摸熟chrome dev tools和html結構 08/27 16:40
→ qxpbyd: PTTLibrary裡的PTT.py 第1291行__getPost 08/27 20:05
→ qxpbyd: 有別人寫好的API 可以參考 08/27 20:05
推 CodingMan: 推 08/28 00:37
→ CodingMan: 資料結構可以看 Test.py 有比較清楚的範例 08/28 00:39
→ b24333666: 我自己寫的應該會符合需求給你參考 09/05 12:19