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. ...
随机推荐
- Ruby 离奇方法
send https://ref.xaio.jp/ruby/classes/object/send find https://ref.xaio.jp/ruby/classes/enumerable/f ...
- codeforces——数学
codeforces 805A http://codeforces.com/problemset/problem/805/A /* 题意:输入两个整数l,r,让你找一个因子 使得[l,r]里面 ...
- 使用内存流导出Excel
public MemoryStream MemoryStreamDeiveFlowInfoaging(DataTable Table) { Dictionary<int , string> ...
- 常见的Java Script内存泄露原因及解决方案
前言 内存泄漏指由于疏忽或错误造成程序未能释放已经不再使用的内存.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制,从而造成了 ...
- c++小游戏
#include <iostream> using namespace std; double shengmingli=2000;//定义主角初始生命力 int gongjili=150; ...
- BZOJ 4481
思路: 等比数列求和 (无穷项) +线段树找逆序对 //By SiriusRen #include <bits/stdc++.h> ; ; ],ans; struct Node{int x ...
- Java中常用的操作PDF的类库
iText iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好的给合.使用 ...
- 个人作业——Alpha项目测试
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 这个作业要求在哪里 https://edu.cnbl ...
- Android studio USB连接失败
Android studio USB连接失败,可能是因为adb的端口被占了,此时在其自带的cmd中输入netstat -aon|findstr "5037",并且启动任务管理器关掉 ...
- jsp 中包含 一个路径为变量的文件
<head> <base href="<%=basePath%>"> <% String fileroot="MyJsp.jsp ...