python (1)一个简单的爬虫: python 在windows下 创建文件夹并写入文件
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下 创建文件夹并写入文件的更多相关文章
- Python写一个简单的爬虫
code #!/usr/bin/env python # -*- coding: utf-8 -*- import requests from lxml import etree class Main ...
- 一个简单的多线程Python爬虫(一)
一个简单的多线程Python爬虫 最近想要抓取拉勾网的数据,最开始是使用Scrapy的,但是遇到了下面两个问题: 前端页面是用JS模板引擎生成的 接口主要是用POST提交参数的 目前不会处理使用JS模 ...
- python爬虫系列(1)——一个简单的爬虫实例
本文主要实现一个简单的爬虫,目的是从一个百度贴吧页面下载图片. 1. 概述 本文主要实现一个简单的爬虫,目的是从一个百度贴吧页面下载图片.下载图片的步骤如下: 获取网页html文本内容:分析html中 ...
- Python并发编程-一个简单的爬虫
一个简单的爬虫 #网页状态码 #200 正常 #404 网页找不到 #502 504 import requests from multiprocessing import Pool def get( ...
- 使用python做最简单的爬虫
使用python做最简单的爬虫 --之心 #第一种方法import urllib2 #将urllib2库引用进来response=urllib2.urlopen("http://www.ba ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 用Python编写一个简单的Http Server
用Python编写一个简单的Http Server Python内置了支持HTTP协议的模块,我们可以用来开发单机版功能较少的Web服务器.Python支持该功能的实现模块是BaseFTTPServe ...
- python中一个简单的webserver
python中一个简单的webserver 2013-02-24 15:37:49 分类: Python/Ruby 支持多线程的webserver 1 2 3 4 5 6 7 8 9 10 11 ...
- Python实现一个简单三层神经网络的搭建并测试
python实现一个简单三层神经网络的搭建(有代码) 废话不多说了,直接步入正题,一个完整的神经网络一般由三层构成:输入层,隐藏层(可以有多层)和输出层.本文所构建的神经网络隐藏层只有一层.一个神经网 ...
随机推荐
- HTML 5 参考手册
HTML5是用于取代1999年所制定的 HTML 4.01 和 XHTML 1.0 标准的 HTML (标准通用标记语言下的一个应用)标准版本:现在仍处于发展阶段,但大部分浏览器已经支持某些 HTML ...
- C# Development 13 Things Every C# Developer Should Know
https://dzone.com/refcardz/csharp C#Development 13 Things Every C# Developer Should Know Written by ...
- T4 Templates
T4 Templates and the Entity Framework https://msdn.microsoft.com/en-us/data/gg558520.aspx EF Designe ...
- 【转】asp.net中的cookie使用介绍
来源:http://www.jb51.net/article/30398.htm 一.cookie导读,理解什么是cookie 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4 ...
- wikioi 1205 单词倒排
/*====================================================================== 1205 单词翻转 题目描述 Description ...
- Spring security与shiro
shiro更轻量级,spring security过于复杂. Apache Shiro 使用手册(一)Shiro架构介绍 Spring Security笔记:Remember Me(下次自动登录)
- 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)
使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...
- Cannot find class for bean with name service
Cannot find class [com.jx.p2p.service.impl.LazyInvestServiceImpl] for bean with name 'service' defin ...
- String equals的技巧
把常量放到前面,可以避免null指针问题 System.out.print("".equals(null)); String abc = null; System.out.prin ...
- httpClenit的post出现乱码问题
在使用httpClient.executeMethod(postMethod)的时候,发现一直存在乱码问题,”book is good“被转成”book+is+good“ 返回.查看源码后,发现pos ...