# coding=utf-8
import re
import requests
import sys
reload(sys)
sys.setdefaultencoding("utf8")
class spider():
def __init__(self):
print "开始爬取内容。。。"
def changePage(self, url, total_page):
nowpage = int(re.search('/0-(\d+)/', url, re.S).group(1))
pagegroup = []
for i in range(nowpage, total_page + 1):
link = re.sub('/0-(\d+)/', '/0-%s/' % i, url, re.S)
pagegroup.append(link)
return pagegroup
def getsource(self, url):
html = requests.get(url)
return html.text
def getclasses(self, source):
classes = re.search('<ul class="zy_course_list">(.*?)</ul>', source, re.S).group(1)
return classes
def geteach(self, classes):
eachclasses = re.findall('<li>(.*?)</li>', classes, re.S)
return eachclasses
def getinfo(self, eachclass):
info = {}
info['title'] = re.search('<a title="(.*?)"', eachclass, re.S).group(1)
info['people'] = re.search('<p class="color99">(.*?)</p>', eachclass, re.S).group(1)
return info
def saveinfo(self, classinfo):
f = open('info.txt', 'a')
for each in classinfo:
f.writelines('title : ' + each['title'] + '\n')
f.writelines('people : ' + each['people'] + '\n\n')
f.close()
if __name__ == '__main__':
classinfo = []
url = 'http://www.maiziedu.com/course/list/all-all/0-1/'
maizispider = spider()
all_links = maizispider.changePage(url, 30)
for each in all_links:
htmlsources = maizispider.getsource(each)
classes = maizispider.getclasses(htmlsources)
eachclasses = maizispider.geteach(classes)
for each in eachclasses:
info = maizispider.getinfo(each)
classinfo.append(info)
maizispider.saveinfo(classinfo)
import requests
html=requests.get("http://gupowang.baijia.baidu.com/article/283878")
html.encoding='utf-8'
print(html.text)
import requests
import os
html=requests.get("http://gupowang.baijia.baidu.com/article/283878")
html_file=open("news.txt","w")
html.encoding='utf-8'
print(html.text,file=html_file)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有