作者MOONY135 (谈无慾)
看板Python
标题Re: [问题] 爬虫如何选取ptt内文内容
时间Fri Jan 13 23:33:39 2017
※ 引述《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://webptt.com/cn.aspx?n=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://webptt.com/cn.aspx?n=bbs/Python/M.1484321622.A.951.html
※ 编辑: MOONY135 (111.248.151.246), 01/13/2017 23:37:24
1F:推 ahahahahah: 谢谢你!我研究一下 01/13 23:43
2F:推 lance8537: 推 01/14 04:39