Python初学小试

2015-04-23 0 594
Python初学小试
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-06-01 10:30:06
# @Author  : HG (hg0728@qq.com)
# @Version : 1.0
import urllib2
import re
import os

def getHtml(url):	#获取html源码
    page = urllib2.urlopen(url)
    html = page.read()
    return html

def urlPages(page):		#翻页
	url = 'http://www.cnblogs.com/sitehome/p/' + str(page)
	#print url
	return url

def findList(html):		#正则匹配列表
    myItems = re.findall('<h3><a class="titlelnk" href="(.*?)" rel="external nofollow"  target="_blank">(.*?)</a></h3>', html, re.S)
    return myItems

for page in range(1, 200+1):	#抓取的页数
	html = getHtml(urlPages(page))
	items = findList(html)
	for item in items:
		s = item[0] +' '+ item[1] + '\n'
		print item[0]
		file_object = open('thefile.txt', 'a')
		file_object.write(s)
		file_object.close()

遇见资源网 python Python初学小试 http://www.ox520.com/15289.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务