python抓取静态网页

lofter的同人文都是一篇一篇的,懒得找,所以就花了点时间写个爬虫,爬取文本数据存储成本地text。这里主要通过lofter的作者专区文章搜索接口地址进行爬取数据。

示例:我是走高冷路线的    该作者的文章搜索地址为:http://sanliubixian.lofter.com/search?q=

后面输入文章名就能搜索到该作者对应的文章。而且还有一个特点,她的文章顺序是根据序号来的,如征服欲1,征服欲2 ...这样,我们就可以进行循环爬取数据了。

1.准备工作

前面踩了很多坑,这里也不一一详细叙述了。我的本地python版本是2.7的。这个注意一下,因为2.7和3.x有一些区别。在这里最主要的区别是使用的urllib模块。这里可以参考一下这位博主。

python 2.xx使用import urllib.request报错no module named request_典笛安的博客-CSDN博客 

第二个就是安装web模块 ,pip install web.py即可安装。

第三个就是编码问题,这里建议使用python的开发工具,我用的是submit text。

其他的就没了,反正就一个py文件,直接上代码吧

 index.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re
import urllib
import urllib2
import web
import json
urls = (
     '/', 'hello'
)
app = web.application(urls, globals())

# 定义函数
def gettext( i ):
    url = 'http://sanliubixian.lofter.com/search?q='
    keyword = i.encode(encoding='utf-8')
    key_code = urllib.quote(keyword)  # 对请求进行编码
    url_all = url+key_code
    header = {
        'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
    }   #头部信息
    request = urllib2.Request(url_all,headers=header)
    reponse = urllib2.urlopen(request).read()

    from bs4 import BeautifulSoup
    html_doc = reponse;


    #创建一个BeautifulSoup解析对象
    soup = BeautifulSoup(html_doc.replace(' ', ' '),"html.parser",from_encoding="utf-8")
    #获取文本
    title = soup.find('h2')
    print title
    if title==None:
        print "全文数据抓取完成!!!"
        return "false"
    else:
        p_nodes = soup.find_all('p')
        fh = open("./"+title.get_text()+".txt","wb")    # 将文件写入到当前目录中
        fh.write(title.get_text().encode(encoding='utf-8'))
        fh.write('\r\n')
        for p_node in p_nodes:
            #print p_node.get_text()
            fh.write(p_node.get_text().encode(encoding='utf-8'))
            fh.write('\r\n')
        fh.close()
        print "抓取:"+title.get_text().encode(encoding='utf-8')
        return "true"


class hello:
    def __init__(self):
        web.header('content-type', 'text/json')
        web.header('Access-Control-Allow-Origin', '*')
        web.header('Access-Control-Allow-Methods', 'GET, POST')
    def GET(self):
        i = web.input(name=None)
        for num in range(1,30):
            s=i.name+str(num)
            result=gettext(s)
            if result=="false":
                break
        
        '''
        t={'msg':'开始爬取数据...','title':title.get_text()}
        s={}
        s['data']=t
        return json.dumps(s,ensure_ascii=False)
        '''

    def POST(self):
        a = int(web.input().a)
        b = int(web.input().b)
        return a + b

if __name__ == "__main__":
    app.run()

运行结果:

 

 码字踩坑不易,转载请注明出处!!谢谢!!

lz初次接触python,自己找资料自己看文档写的,如有不专业之处,还请专业人士见谅

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值