#-*-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的更多相关文章

  1. 爬取软考试题系列之ip自动代理

    马上5月份有个软件专业等级考试,以下简称软考,为了更好的复习备考,我打算抓取www.rkpass.com网上的软考试题. 以上为背景. 很久没有更新博客园的博客了,所以之前的代码没有及时的贴出来,咱们 ...

  2. 使用Node.js实现简单的网络爬取

    由于最近要实现一个爬取H5游戏的代理服务器,隧看到这么一篇不错的文章(http://blog.miguelgrinberg.com/post/easy-web-scraping-with-nodejs ...

  3. Python:爬取网站图片并保存至本地

    Python:爬取网页图片并保存至本地 python3爬取网页中的图片到本地的过程如下: 1.爬取网页 2.获取图片地址 3.爬取图片内容并保存到本地 实例:爬取百度贴吧首页图片. 代码如下: imp ...

  4. python Requests库网络爬取IP地址归属地的自动查询

    #IP地址查询全代码import requestsurl = "http://m.ip138.com/ip.asp?ip="try: r = requests.get(url + ...

  5. Python Requests库网络爬取全代码

    #爬取京东商品全代码 import requestsurl = "http://item.jd.com/2967929.html"try: r = requests.get(url ...

  6. Python3爬虫(1)_使用Urllib进行网络爬取

    网络爬虫 又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者,是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕虫 ...

  7. Python-爬虫实战 简单爬取豆瓣top250电影保存到本地

    爬虫原理 发送数据 获取数据 解析数据 保存数据 requests请求库 res = requests.get(url="目标网站地址") 获取二进制流方法:res.content ...

  8. python爬虫学习(二):定向爬虫例子-->使用BeautifulSoup爬取"软科中国最好大学排名-生源质量排名2018",并把结果写进txt文件

    在正式爬取之前,先做一个试验,看一下爬取的数据对象的类型是如何转换为列表的: 写一个html文档: x.html<html><head><title>This is ...

  9. Python使用requests爬取一个网页并保存

    #导入 requests模块import requests #设置请求头,让网站监测是浏览器 headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 6. ...

随机推荐

  1. ios14--购物车优化2

    // // ViewController.m // 03-综合练习 // // Created by xiaomage on 15/12/28. // Copyright © 2015年 小码哥. A ...

  2. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  3. 洛谷P2516 [HAOI2010]最长公共子序列

    题目描述 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X="x0,x1,-,xm-1",序列Y=& ...

  4. SmartDispatcher 类

    UI线程中使用 public class SmartDispatcher { public static void BeginInvoke(Action action) { if (Deploymen ...

  5. E20170919-hm

    infinity   n. <数>无穷大; 无限的时间或空间;

  6. E20170627-gg

    ring   n. 戒指,指环; 铃声,钟声; 环形物; 拳击场;   vi. 按铃,敲钟; 回响; 成环形; rear   n. 后部,背面,背后; 臀部; (舰队或军队的) 后方,后尾,殿后部队; ...

  7. Gym - 102059D 2018-2019 XIX Open Cup, Grand Prix of Korea D. Dumae 贪心+堆

    题面 题意:有3e5个人排成一列,然后Li,Ri表示每个人可以站在[Li,Ri]中的一个,然后M(1e6)个限制条件,某个人一定要在某个人前面,求一种合法方案,无解输出-1 题解:首先可以想到对于限制 ...

  8. [Swift通天遁地]四、网络和线程-(9)上传图片并实时显示上传进度

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. JavaScript--String 字符串对象属性

    访问字符串对象的属性length: stringObject.length; 返回该字符串的长度. var mystr="Hello World!"; var myl=mystr. ...

  10. Axure基础教程

    文字教程:      https://www.axure.com.cn/3608/ 视频教程:      http://www.iqiyi.com/playlist409963402.html