使用python从360doc上抓取内容

#人生苦短,我用python
在360doc上查到一篇不错的文章,准备复制收藏的时候,提示让我登录。
我没有帐号,只好祭出python,顺利拿到数据,自动根据网页标题,保存成html页面
提示:360doc服务端,检查了header,代码里已经添加了。

import urllib.request
from urllib import request
import os

hosturl = 'http://www.360doc.com/content/14/0813/17/15477063_401589947.shtml'

#构造header,一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',
'Referer' : hosturl}
#打开登录主页面(他的目的是从页面下载cookie,这样我们在再送post数据时就有cookie了,否则发送不成功)
request = urllib.request.Request(hosturl, None, headers)
response = urllib.request.urlopen(request)
htmldata = response.read()
data=htmldata.decode('utf-8')

pre = '<span id="articlecontent" onmousedown="newhighlight = true;" onmouseup="NewHighlight(event)">'
index1 = data.find(pre) + len(pre)
index2 = data.find('<div id="viewerPlaceHolder" style="width: 717px; height: 700px; display: none;">', index1)
content = data[index1 : index2]


pretitle = '<div class="biaoti2 lf360">'
indextitle1 = data.find(pretitle) + len(pretitle)
indextitle2 = data.find('</div>', indextitle1)
title = data[indextitle1 : indextitle2].replace("\r\n","").strip()

print(title)
f = open(title+"_"+hosturl.split("/")[-1],"w")
f.write(content)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值