1.一个简单的爬虫:爬取豆瓣的热门电影的信息

写在前面:如何创建本来存在的文件夹并写入

 t_path = "d:/py/inn"            #本来不存在inn,先定义路径,然后如果不存在,则创建目录,下面就能用了
if not os.path.exists(t_path):
os.makedirs(t_path)
f = open(r'd:/py/inn/info.txt','a')

技能:获取网页源码,正则表达式,函数调用,全局变量的定义

 #! /usr/bin/env python
# -*- coding=utf-8 -*-
import requests
import json
import re
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
classinfo = []
f = open('info.txt','w') num = 0
def write(htm):
titl = re.findall('data-tit(.*?)data-enough',htm.text,re.S)
for each in titl:
#print each
info = {}
#print each
info['title'] = re.search('le="(.*?)"',each,re.S).group(1)
info['year'] = re.search('data-release="(.*?)" data',each,re.S).group(1)
info['Rating']= re.findall('data-rate="(.*?)" data-star',each,re.S)[0]
info['time'] = re.findall('data-duration="(.*?)" data-re',each,re.S)[0]
info['reg'] = re.findall('data-region="(.*?)" data-dir',each,re.S)[0]
info['act'] = re.findall('data-actors="(.*?)" data-in',each,re.S)[0]
global num #全局的定义
num = num + 1
f.writelines('%d\n' %num)
f.writelines(u'电影名:'+info['title'] + '\n')
f.writelines(u'主演:'+info['act'] + '\n')
f.writelines(u'电影地区:' + info['reg']+'\n')
f.writelines(u'上映年份:' + info['year']+'\n')
f.writelines(u'电影时长:' + info['time']+'\n')
f.writelines(u'评分:' + info['Rating']+'\n\n')
def getremen():
# html = requests.get('http://movie.douban.com/')
url = 'http://movie.douban.com/'
html = requests.get(url)
html.encoding = 'utf-8'
# print html.text
write(html)
if __name__ == "__main__":
getremen()

python (1)一个简单的爬虫: python 在windows下 创建文件夹并写入文件的更多相关文章

  1. Python写一个简单的爬虫

    code #!/usr/bin/env python # -*- coding: utf-8 -*- import requests from lxml import etree class Main ...

  2. 一个简单的多线程Python爬虫(一)

    一个简单的多线程Python爬虫 最近想要抓取拉勾网的数据,最开始是使用Scrapy的,但是遇到了下面两个问题: 前端页面是用JS模板引擎生成的 接口主要是用POST提交参数的 目前不会处理使用JS模 ...

  3. python爬虫系列(1)——一个简单的爬虫实例

    本文主要实现一个简单的爬虫,目的是从一个百度贴吧页面下载图片. 1. 概述 本文主要实现一个简单的爬虫,目的是从一个百度贴吧页面下载图片.下载图片的步骤如下: 获取网页html文本内容:分析html中 ...

  4. Python并发编程-一个简单的爬虫

    一个简单的爬虫 #网页状态码 #200 正常 #404 网页找不到 #502 504 import requests from multiprocessing import Pool def get( ...

  5. 使用python做最简单的爬虫

    使用python做最简单的爬虫 --之心 #第一种方法import urllib2 #将urllib2库引用进来response=urllib2.urlopen("http://www.ba ...

  6. 用Python写一个简单的Web框架

    一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...

  7. 用Python编写一个简单的Http Server

    用Python编写一个简单的Http Server Python内置了支持HTTP协议的模块,我们可以用来开发单机版功能较少的Web服务器.Python支持该功能的实现模块是BaseFTTPServe ...

  8. python中一个简单的webserver

     python中一个简单的webserver 2013-02-24 15:37:49 分类: Python/Ruby 支持多线程的webserver   1 2 3 4 5 6 7 8 9 10 11 ...

  9. Python实现一个简单三层神经网络的搭建并测试

    python实现一个简单三层神经网络的搭建(有代码) 废话不多说了,直接步入正题,一个完整的神经网络一般由三层构成:输入层,隐藏层(可以有多层)和输出层.本文所构建的神经网络隐藏层只有一层.一个神经网 ...

随机推荐

  1. click 绑定(三)防止事件冒泡

     防止事件冒泡 默认情况下,Knockout允许click事件继续在更高一层的事件句柄上冒泡执行.例如,如果你的元素和父元素都绑定了click事件,那当你点击该元素的时候两个事件都会触发的.如果需要, ...

  2. MAC 如何修改PATH

    http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac

  3. 编写linux驱动所用到的头文件(转)

    转自:http://blog.csdn.net/lufeiop02/article/details/6448497 关于linux驱动(应用)程序头文件使用 收藏 驱动程序: #include < ...

  4. java的nio之:java的nio的原理

    转载:http://weixiaolu.iteye.com/blog/1479656 Java NIO原理图文分析及代码实现 前言: 最近在分析hadoop的RPC(Remote Procedure ...

  5. linux查找目录下的所有文件中是否含有某个字符串

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" find .|xargs grep -ri "IBM" -l ...

  6. 将caffe训练时loss的变化曲线用matlab绘制出来

    1. 首先是提取 训练日志文件; 2. 然后是matlab代码: clear all; close all; clc; log_file = '/home/wangxiao/Downloads/43_ ...

  7. IntelliJ IDEA currently

    https://www.jetbrains.com/help/idea/2016.2/creating-a-project-from-scratch.html https://www.jetbrain ...

  8. Opencv 3入门(毛星云)摘要

    第一章 环境搭建: 1.  环境变量path 添加  D:\Program Files\opencv\build\x86\vc11\bin 2.  VS在VC++项目中,属性管理器\属性. VC++目 ...

  9. Xcode 7 ImageNamed 方法加载jpg图片失败

    更新XCode7后 原来的Image.xcassets文件夹变成了Assets.xcassets 把01.jpg,02.jpg,03.png拖入这个文件夹中 UIImage* test1=[UIIma ...

  10. [Unity 5.2] The imported type `UnityEngine.Advertisements.ShowResult' is defined multiple times

    unityAds报这个错: The imported type `UnityEngine.Advertisements.ShowResult' is defined multiple times go ...