[问题] 抓取留言

楼主: s1021412107 (阿宗)   2018-08-27 13:33:54
请问各位大大
小弟刚学爬虫
想要抓取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
作者: InfinityGate (小鸟)   2018-08-27 16:38:00
把div.push里面的span都抓下来啊爬虫先摸熟chrome dev tools和html结构
作者: qxpbyd (qxpbyd)   2018-08-27 20:01:00
https://github.com/Truth0906/PTTLibraryPTTLibrary里的PTT.py 第1291行__getPost有别人写好的API 可以参考
作者: CodingMan (程式侠)   2018-08-28 00:37:00
推资料结构可以看 Test.py 有比较清楚的范例
作者: b24333666 (比飞笨)   2018-09-05 12:18:00
https://goo.gl/vV8QgY我自己写的应该会符合需求给你参考

Links booklink

Contact Us: admin [ a t ] ucptt.com