简单爬虫 爬知乎日报

原文链接: 简单爬虫 爬知乎日报

上一篇: 爬虫下载文章 BeautifulSoup

下一篇: Python 学生管理

# coding=utf-8
import urllib
import urllib2
import urlparse
import re
import bs4
import requests

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

url = 'https://daily.zhihu.com/'


# 拿到网页源码
def getHtml(url):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
    }

    request = urllib2.Request(url, headers=header)
    response = urllib2.urlopen(request)
    text = response.read()
    return text


# 拿到超链接
def getUrls(html):
    # 正则表达式,提高效率re.s匹配换行符
    pattern = re.compile('<a href="/story/(.*?)"', re.S)
    items = re.findall(pattern, html)

    urls = []
    for i in items:
        urls.append('https://daily.zhihu.com/story/' + i)

        # print urls
    return urls


# 解析标题和内容
def getContent(url):
    html = getHtml(url)
    # <title>在一件事情上付出越多,你对它就越喜欢,真怪</title>
    pattern = re.compile('<title>(.*?)</title>', re.S)
    titles = re.findall(pattern, html)

    # 中文在可迭代对象中是Unicode编码,
    # print  titles[0]


    pattern = re.compile('<div class="content">(.*?)<div class="view-more">', re.S);
    content = re.findall(pattern, html)[0]

    return '<h1>'+titles[0]+'/h1' + '\n' + content

#去除不需要的标签
def clear(html):
    pattern= re.compile('<p>(.*?)</p>|<li>(.*?)</li> ')
    items = re.findall(pattern,html)
    result = []

html = getHtml(url)
urls = getUrls(html)
with open('out.html', 'w+') as f:
    f.write(getContent(urls[0]))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值