pyhton 网络爬取软考题库保存text
#-*-coding:utf-8-*-
#参考文档
#https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all
import requests
import re
from bs4 import BeautifulSoup
html = requests.get('https://m.cnitpm.com/exam/ExamST1_1031655.htm/')
soup = BeautifulSoup(html.text,'lxml')
ulTag=soup.find_all('ul','tit')
#计数器每次调用加1
def create_counter():
def increase(): #定义一个还有自然数算法的生成器,企图使用next来完成不断调用的递增
n = 0
while True:
n = n+1
yield n
it = increase() #一定要将生成器转给一个(生成器)对象,才可以完成,笔者第一次做,这里一直出问题,
def counter(): #再定义一内函数
return next(it) #调用生成器的值,每次调用均自增
return counter
counter_ = create_counter() #用变量来指向(闭包函数返回的函数)
#保存文件
def save(filename, contents):
#print(counter_())
fh = open(filename, 'a+', encoding='utf-8')
#print(str(counter_())+contents.strip()+'\n')
fh.write(str(counter_())+'、'+contents.strip()+'\n')
fh.close() ###################################网络爬取页面分析
for item in ulTag:
a_temp=item.find_all('a')
#print(a_temp)
for aitem in a_temp:
#print (aitem.get('href'))
html2 = requests.get(aitem.get('href'))
#解决乱码问题
html2.encoding = 'utf-8'
soup2 = BeautifulSoup(html2.text, 'lxml')
divTag = soup2.find_all('div', 'tm-box')
for divitem in divTag:
#print(divitem.get_text())
#保存到文档
save('G:/aa/qa.txt',divitem.get_text())
#print(divTag.replace('[<div class="tm-box">', ''))
pyhton 网络爬取软考题库保存text的更多相关文章
- 爬取软考试题系列之ip自动代理
马上5月份有个软件专业等级考试,以下简称软考,为了更好的复习备考,我打算抓取www.rkpass.com网上的软考试题. 以上为背景. 很久没有更新博客园的博客了,所以之前的代码没有及时的贴出来,咱们 ...
- 使用Node.js实现简单的网络爬取
由于最近要实现一个爬取H5游戏的代理服务器,隧看到这么一篇不错的文章(http://blog.miguelgrinberg.com/post/easy-web-scraping-with-nodejs ...
- Python:爬取网站图片并保存至本地
Python:爬取网页图片并保存至本地 python3爬取网页中的图片到本地的过程如下: 1.爬取网页 2.获取图片地址 3.爬取图片内容并保存到本地 实例:爬取百度贴吧首页图片. 代码如下: imp ...
- python Requests库网络爬取IP地址归属地的自动查询
#IP地址查询全代码import requestsurl = "http://m.ip138.com/ip.asp?ip="try: r = requests.get(url + ...
- Python Requests库网络爬取全代码
#爬取京东商品全代码 import requestsurl = "http://item.jd.com/2967929.html"try: r = requests.get(url ...
- Python3爬虫(1)_使用Urllib进行网络爬取
网络爬虫 又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者,是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕虫 ...
- Python-爬虫实战 简单爬取豆瓣top250电影保存到本地
爬虫原理 发送数据 获取数据 解析数据 保存数据 requests请求库 res = requests.get(url="目标网站地址") 获取二进制流方法:res.content ...
- python爬虫学习(二):定向爬虫例子-->使用BeautifulSoup爬取"软科中国最好大学排名-生源质量排名2018",并把结果写进txt文件
在正式爬取之前,先做一个试验,看一下爬取的数据对象的类型是如何转换为列表的: 写一个html文档: x.html<html><head><title>This is ...
- Python使用requests爬取一个网页并保存
#导入 requests模块import requests #设置请求头,让网站监测是浏览器 headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 6. ...
随机推荐
- ubuntu安装ruby,安装sass,安装compass
安装ruby apt-get install ruby. 默认会安装1.9的. 不是自己想要的. 可以进行下面的处理. # sudo apt-get install python-software-p ...
- bzoj 3944 Sum —— 杜教筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3944 杜教筛入门题! 看博客:https://www.cnblogs.com/zjp-sha ...
- ubuntu16.04 Flume 安装
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并 ...
- astgo 老版本免激活完整安装包带安装命令脚本
astgo是个国产的老牌经典软交换服务器,主要用来当回拨网络电话服务端,同时也具备群呼.传真等功能! 这个需要安装在centos 5.x 32位系统.带安装脚本,上传到root目录后执行安装脚本即可等 ...
- yum 安装redis
1.yum install redis 2.yum install php-redis 3service redis start chkconfig redis on
- Akka源码分析-Remote-ActorSystem
前面的文章都是基于local模式分析的,现在我们简要分析一下在remote模式下,ActorSystem的创建过程. final val ProviderClass: String = setup.g ...
- BZOJ 1208 set
思路: 开俩set 模拟一下 就好了 //By SiriusRen #include <bits/stdc++.h> using namespace std; int n,xx,yy; s ...
- mysql使用出现错误:mysqld dead but subsys locked
现象: 登陆时候出现如下信息 [root@localhost home]# mysql -uroot -p123456mysql: [Warning] Using a password on the ...
- 再战primer——decltype 和引用
刷primer看到原文讲到"引用从来都作为其所指对象的同义词出现,只有用在decltype处是一个例外.",我很是好奇. 这个“引用”当然是指引用类型,like this: ; i ...
- Echarts 出现不明竖线解决方案
Echarts出现了不明竖线,百思不得其解.去查相应的解决方案也没有找到. 后来自己点来点去,突然感觉像是上一个Echarts遗留的. 然后去Echarts官网看到了 clear()方法,这个方法可以 ...