作者:破晓晨曦 | 来源:互联网 | 2017-05-14 02:43
php中文网(www.php.cn)提供了最全的编程技术基础教程,介绍了HTML、CSS、Javascript、Python,Java,Ruby,C,PHP,MySQL等各种编程语言的基础知识。同时本站中也提供了大量的在线实例,通过实例,您可以更好的学习编程。..
回复内容:
context_re = r'(.*?)
'
你准备的这个正则表达式啊,truncated!断在了
这里,所以只能爬第一段。
爬取新浪军事论坛需要做三件事:
一、上CSDN汪海老师的专栏,
http://blog.csdn.net/column/details/why-bug.html,学习一个。
二、按F12看一下前端。
三、from bs4 import BeautifulSoup
import requests
response = requests.get("http://club.mil.news.sina.com.cn/thread-666013-1-1.html?retcode=0") #硬点网址
response.encoding = 'gb18030' #中文编码
soup = BeautifulSoup(response.text, 'html.parser') #构建BeautifulSoup对象
ps = soup('p', 'mainbox')
#每个楼层
for p in ps:
comments = p.find_all('p','cont f14') #每个楼层的正文
with open('Sina_Military_Club.txt','a') as f:
f.write('\n'+str(comments)+'\n')
刚好几个小时前就在写一个爬取网站会员(公司)资料的小程序
具体的编程问题就不回答了,跟用什么语言写代码无关,关键是你要分析好这个页面的html代码结构,写出合适的正则表达式来进行匹配,如果想简化的话,可以进行分次匹配(比如先得到
里面的第一个
里面的内容就是原帖的地址,然后再进一步处理)
大数据分析就不会了,还请赐教。
import requests
from bs4 import BeautifulSoup
r = requests.get("http://club.mil.news.sina.com.cn/thread-666013-1-1.html")
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text)
result = soup.find(attrs={"class": "cont f14"})
print result.text
用beautifulSoup吧,正则太多了看着都头疼.
先用了BeautifulSoup爬取数据
# -*- coding:utf-8 -*-
import re, requests
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
url = "http://club.mil.news.sina.com.cn/viewthread.php?tid=666013&extra=page%3D1&page=1"
req = requests.get(url)
req.encoding = req.apparent_encoding
html = req.text
soup = BeautifulSoup(html)
file = open('sina_club.txt', 'w')
x = 1
for tag in soup.find_all('p', attrs = {'class': "cont f14"}):
word = tag.get_text()
line1 = "---------------评论" + str(x) + "---------------------" + "\n"
line2 = word + "\n"
line = line1 + line2
x += 1
file.write(line)
file.close()
哎,扒就扒吧,发了paper能不能告诉我刊号页数让我看一下?我们自己都没做大数据分析……
建议用一下正则测试工具
你需要pyquery,可以使用jquery一样的语法。你值得拥有。
https://pythonhosted.org/pyquery/