Python 网络爬虫单线程版

简介: re.S让.能够匹配\n,默认情况点是不能匹配换行符的1.爬取网页源码中的图片#-*- coding:utf-8 -*-import reimport requestswith open('source.txt', 'r') as f:    html = f.read()#匹配图片网址,括号中为需要返回的内容pic_url = re.findall('img src="(

re.S让.能够匹配\n,默认情况点是不能匹配换行符的


1.爬取网页源码中的图片

#-*- coding:utf-8 -*-
import re
import requests
with open('source.txt', 'r') as f:
    html = f.read()

#匹配图片网址,括号中为需要返回的内容
pic_url = re.findall('img src="(.*?)" class="lessonimg"', html, re.M)
i = 0
for each in pic_url:
    print "now downloading:"+each
    pic = requests.get(each)
    fp = open('pic\\'+str(i)+'.jpg', 'wb')
    fp.write(pic.content)
    fp.close()
    i += 1


2.突破反爬虫机制伪装成浏览器设置headers

#-*- coding:utf-8 -*-

import requests
import sys
import re
#很多情况下sys.defaultencoding是ascii
reload(sys)
sys.setdefaultencoding("utf-8")
type = sys.getdefaultencoding()
print type

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:42.0) Gecko/20100101 Firefox/42.0'}
html = requests.get('http://jp.tingroom.com/yuedu/yd300p/', headers=headers)
html.encoding = 'utf-8'
print html.text

for japanese in re.findall('<span style="color:#666666;">(.*?)</span>', html.text, re.S):
    print japanese

for chinese in re.findall('style="color: #039;">(.*?)</a>', html.text, re.S):
    print chinese


3.发起post请求

#-*- coding:utf-8 -*-
import re
import requests


url = 'http://www.crowdfunder.com/browse/deals&template=false'
data = {
    'entities_only': 'true',
    'page': '3'
}
html_post = requests.post(url, data=data)

title = re.findall('class="card-title">(.*?)</div>', html_post.text, re.S)
print title
for each in title:
    print each


4.爬取极客学院课程详细信息

re.search匹配第一个

re.findall匹配所有

# coding=utf-8
__author__ = 'scaleworld'
import requests
import re
import sys

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


class Spider(object):
    def __init__(self):
        print '开始爬取极客学院课程信息。。。'

    # 获取源代码
    def getsource(self, url):
        html = requests.get(url)
        return html.text

    # 获取每个课程块信息
    def getlessons(self, source):
        lessons = re.findall('deg="0" >(.*?)</li>', source, re.S)
        return lessons

    # 获取课程信息,如课程名称、课程介绍、课程时间、课程等级、学习人数
    def getlessonInfo(self, lesson):
        info = {}
        info['title'] = re.search('<h2 class="lesson-info-h2"><a(.*?)>(.*?)</a></h2>', lesson, re.S).group(2).strip()
        info['desc'] = re.search('<p style="height: 0px; opacity: 0; display: none;">(.*?)</p>', lesson, re.S).group(
            1).strip()
        timeandlevel = re.findall('<em>(.*?)</em>', lesson, re.S)
        info['time'] = timeandlevel[0].strip().replace("\n", "").replace("    ", "")
        info['level'] = timeandlevel[1].strip()
        info['learnNumber'] = re.search('"learn-number">(.*?)</em>', lesson, re.S).group(1).strip()
        return info

    # 保存课程信息到文件LessionInfos.txt
    def savelessionInfos(self, lessonInfos):
        # 'w':只写,会覆盖之前写入的内容
        # 也可以用'a':追加到文件末尾
        # 如果文件不存在,则自动创建文件
        f = open('LessionInfos.txt', 'w')
        i = 0
        for each in lessonInfos:
            i += 1
            f.writelines('第' + str(i) + '个课程:\n')
            f.writelines('title:' + each['title'] + '\n')
            f.writelines('desc:' + each['desc'] + '\n')
            f.writelines('time:' + each['time'] + '\n')
            f.writelines('level:' + each['level'] + '\n')
            f.writelines('learnNumber:' + each['learnNumber'] + '\n\n')
        f.close()


if __name__ == '__main__':
    # 定义课程信息数组
    lessonInfos = []
    # 课程信息页面url
    url = 'http://www.jikexueyuan.com/course/'
    # 实例化爬虫
    spider = Spider()
    # 取[1,21)及1到20页的课程信息
    for i in range(1, 21):
        # 构建分页URL
        pageUrl = url + '?pageNum=' + str(i)
        print '正在处理页面:' + pageUrl
        source = spider.getsource(pageUrl)
        lessons = spider.getlessons(source)
        for lesson in lessons:
            lessonInfo = spider.getlessonInfo(lesson)
            lessonInfos.append(lessonInfo)
            # print 'title:'+lessonInfo.get('title')  #函数返回指定键的值,如果值不在字典中返回默认值,不会报异常
            # print 'desc:'+lessonInfo.get('desc')
            # print 'time:'+lessonInfo.get('time')
            # print 'level:'+lessonInfo.get('level')
            # print 'learnNumber:'+lessonInfo.get('learnNumber')
        print '已处理' + str(lessons.__len__()) + '个课程信息。'
    print '极客学院课程信息爬取完毕,正在保存课程信息。。。'
    spider.savelessionInfos(lessonInfos)
    print '极客学院课程信息保存完毕。'



本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1726527

目录
相关文章
|
5天前
|
负载均衡 算法 安全
基于Reactor模式的高性能网络库之线程池组件设计篇
EventLoopThreadPool 是 Reactor 模式中实现“一个主线程 + 多个工作线程”的关键组件,用于高效管理多个 EventLoop 并在多核 CPU 上分担高并发 I/O 压力。通过封装 Thread 类和 EventLoopThread,实现线程创建、管理和事件循环的调度,形成线程池结构。每个 EventLoopThread 管理一个子线程与对应的 EventLoop(subloop),主线程(base loop)通过负载均衡算法将任务派发至各 subloop,从而提升系统性能与并发处理能力。
29 3
|
9天前
|
数据采集 存储 JSON
Python爬取知乎评论:多线程与异步爬虫的性能优化
Python爬取知乎评论:多线程与异步爬虫的性能优化
|
1月前
|
数据采集 存储 Web App开发
Python爬虫技巧:设置Cookie永不超时的详细指南
Python爬虫技巧:设置Cookie永不超时的详细指南
|
8天前
|
数据采集 存储 数据库
Python爬虫开发:Cookie池与定期清除的代码实现
Python爬虫开发:Cookie池与定期清除的代码实现
|
1天前
|
数据采集 存储 监控
Python爬虫自动化:定时监控快手热门话题
Python爬虫自动化:定时监控快手热门话题
|
7天前
|
数据采集 机器学习/深度学习 边缘计算
Python爬虫动态IP代理报错全解析:从问题定位到实战优化
本文详解爬虫代理设置常见报错场景及解决方案,涵盖IP失效、403封禁、性能瓶颈等问题,提供动态IP代理的12种核心处理方案及完整代码实现,助力提升爬虫系统稳定性。
38 0
|
1月前
|
数据采集 机器学习/深度学习 Web App开发
Python爬虫如何应对贝壳网的IP封禁与人机验证?
Python爬虫如何应对贝壳网的IP封禁与人机验证?
|
1月前
|
数据采集 Web App开发 JavaScript
无头浏览器技术:Python爬虫如何精准模拟搜索点击
无头浏览器技术:Python爬虫如何精准模拟搜索点击
|
23天前
|
数据采集 网络协议 前端开发
Python多线程爬虫模板:从原理到实战的完整指南
多线程爬虫通过并发请求大幅提升数据采集效率,适用于大规模网页抓取。本文详解其原理与实现,涵盖任务队列、线程池、会话保持、异常处理、反爬对抗等核心技术,并提供可扩展的Python模板代码,助力高效稳定的数据采集实践。
44 0

推荐镜像

更多