情况如下,
现在有一个填写注册信息的页面,当一个用户注册完成之后我需要把他填写的注册信息
另存一份在我自己建的数据库中,这样的功能应该怎么实现呀,我萌新小白,python语言
刚刚入门,想请教一下这样的问题的思路,欢迎各位大佬指点
在线急!需要实现一个网页爬虫的程序
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 悟了个空 2017-10-10 08:43关注
之前也是了解了一下Python, 你参考一下吧, 可以直接运行
#coding:utf-8 import urllib2, re #第一页 http://www.neihan8.com/article/index.html #其他页码 http://www.neihan8.com/article/index_[x].html #匹配规则 #得到段子的url路径 <h3><a href="()" #得到段子标题的 <h1 class="title">()</h1> #得到段子内容的 </a></p>()<div class="ad610"> class Spider: '''爬虫类''' def __init__(self): #当前爬虫需要爬取的页码 self.page = 1 def load_page(self, url): '''加载某个页面 得到全部该页码源码''' #模拟火狐浏览器 user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0" header= {"User-Agent": user_agent} request = urllib2.Request(url, headers = header) #发送url请求 response = urllib2.urlopen(request) #得到返回数据 html_text = response.read() return html_text def spider_one_page(self): '''爬取第page页,同时得到该页的全部端子的url列表''' if (self.page == 1): url = "http://www.neihan8.com/article/index.html" else: url = "http://www.neihan8.com/article/index_"+str(self.page)+".html" html_text = self.load_page(url) #print html_text #得到全部的 段子的 url地址 pattern = re.compile('<h3><a href="(.*?)"', re.S) dz_url_list = pattern.findall(html_text) print dz_url_list return dz_url_list def save_title_content_list(self, titles, contents): '''将标题和内容写到文件中''' print "正在保存 第 " + str(self.page) +"页的全部端子" for i in range(len(titles)): contents[i] = contents[i].replace("\r\n", "")\ .replace("<div>", "")\ .replace("</div>", "")\ .replace("<p>", "")\ .replace("</p>", "")\ .replace("“", "")\ .replace("”", "")\ .replace(" ", "")\ .replace("…", "") self.write_one_dz(titles[i], contents[i]) def spider_dzurl_list(self, url_list): '''根据url_list 分别请求段子的网页,爬取段子的内容''' titles = [] contents = [] for url in url_list: dz_url = "http://www.neihan8.com"+url html_text = self.load_page(dz_url) #得到标题 pattern = re.compile('<h1 class="title">(.*?)</h1>', re.S) title = pattern.findall(html_text) titles.append(title[0]) #得到标题 pattern = re.compile('</a></p>(.*?)<div class="ad610">', re.S) content = pattern.findall(html_text) contents.append(content[0]) #将标题和数据 保存到 文件中 self.save_title_content_list(titles, contents) def write_one_dz(self, title, content): fp = open('./myDuanzi.txt', "a") fp.write("="*15) fp.write("\n") fp.write(title) fp.write("\n") fp.write("+"*15) fp.write("\n") fp.write(content) fp.write("\n") fp.close() def doWork(self): '''爬虫的主业务方法''' while True: print "输入回车 爬取下一页" print "输入的exit退出" cmd = raw_input() if (cmd == "exit"): break #开始爬取 dz_url_list = self.spider_one_page() self.spider_dzurl_list(dz_url_list) #爬取该页完毕 print "爬取 " + str(self.page) +" 页 完毕" self.page += 1 if __name__=="__main__": sp = Spider() sp.doWork()
解决 无用评论 打赏 举报
悬赏问题
- ¥15 delta降尺度计算的一些细节,有偿
- ¥15 Arduino红外遥控代码有问题
- ¥15 数值计算离散正交多项式
- ¥30 数值计算均差系数编程
- ¥15 redis-full-check比较 两个集群的数据出错
- ¥15 Matlab编程问题
- ¥15 训练的多模态特征融合模型准确度很低怎么办
- ¥15 kylin启动报错log4j类冲突
- ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
- ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序