Python爬虫框架
本文章的源代码来源于https://github.com/Holit/Web-Crawler-Framwork
一、爬虫框架的代码
import urllib.request
from bs4 import BeautifulSoup
import re
import time
import _thread # Input your Url here####################################
BaseURL = '127.0.0.1/'
#########################################################
TaxURL = ".html" #Input your data-saving path ############################
SavePath = ""
######################################################### #Input your threads count ###############################
thread_count = 1
######################################################### #Set each spider will spy how many pages ################
thread_spy_count_ench = 5
#########################################################
def mkdir(path):
# Create the directory
import os
path=path.strip()
path=path.rstrip("\\")
isExists=os.path.exists(path)
if not isExists:
os.makedirs(path)
return True
else:
return False def download(start, count):
#Spider main
for i in range(start,start + count):
try:
#DEBUG##################################################
#print("[INFO] Connecting to page #" + str(i) + "...")
######################################################## #Used to record time
time_start=time.time() #Construct url
#This only work like
# https://127.0.0.1/articles/00001.html
# https://127.0.0.1/articles/00002.html
# https://127.0.0.1/articles/00003.html
TargetURL = BaseURL + str(i) + TaxURL #create Request object
req = urllib.request.Request(TargetURL)
#create headers using general header, you could find this by Fiddler(R) or by Chrome(R)
req.add_header('Host','') #Your Host, usally set as url-base
req.add_header('Referer',TargetURL) #Your Referer, usally set as url
req.add_header('User-Agent', 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19')
#finishing create Request object #get information
res = urllib.request.urlopen(req)
#decode the html
soup = BeautifulSoup(res,"html.parser")
##############################################################
#add your functions here.... #operate_data(data) #soup find div to get inforamtion.
#soup is able to opreate html tag very easily, by using soup.find(...)
############################################################## #Change saving path here.
savetarget = SavePath #trying to saving files
try:
#create directory if it doesn't existed
mkdir(SavePath+"\\"+str(zone)+"\\")
#using open...
f = open(savetarget,'w') #edit this
f.write("data") except Exception as e:
time_end=time.time()
print(" [Failed] - #" + str(i) + " Error : " + str(e))
else: time_end=time.time()
print(" [Succeed] - #" + str(i) + " has saved to path.("+str(time_end-time_start)+"s)") pass
except Exception as e:
print(" [Global Failure] - #" + str(i) + " Error : " + str(e))
pass #if __name__ == __main__:
try:
#Multithreading
print("Spidering webiste...")
print("Current configuration :")
print("--Will create " + str(thread_count) + "threads to access.")
print("--Will save to " + SavePath)
print("-------------START---------------------------")
# press any key to continue
# this won't work under linux
import os
os.system('pause')
try:
for i in range(0,thread_count):
print("[Thread #"+ str (i) +"] started successfully")
_thread.start_new_thread(download, (thread_spy_count_ench * i,thread_spy_count_ench))
except Exception as e:
print("[Threading@" + str(i) +"] Error:"+ str(e))
except Exception as e:
print("[Global Failure] Error:"+ str(e))
while 1:
pass
二、对其中功能的实例化操作
1.文本获取功能
文本获取是指对页面的<div class='content'>...</div>中的内容进行获取,这是前提。如果不同需要更改。
(1)思路
使用BeautifulSoup对html分析之后得到解码的文件,例如
<div class="content" style="text-align: left">
基础内容
</div>
现在对该段落进行选取,即使用soup.find功能
(2)基本代码
passages_div = soup.find('div')
passages_set = passages_div.findAll(attrs={"class":"content"})
for passages in passages_set:
article = str(passages)
#文字处理
article = article.replace('<div class="content" style="text-align: left">', '')
article = article.replace(u'\ue505', u' ')#对Unicode的空格进行处理,如果不处理gbk无法编码
article = article.replace(u'\ue4c6', u' ')
article = article.replace(u'\xa0', u' ')
article = article.replace('<br/>', '\n')
article = article.replace('</div>', '')
savetarget = 'D:\test\test.txt'
try:
mkdir('D:\test\')
f = open(savetarget,'w')
f.write(article)
except Exception as e:
print(" [Failed] - "+ str(e))
else:
time_end=time.time()
print(" [Succeed] - saved to path.")
pass
2.图片获取操作
图片获取一般是通过对网页上的<img src="127.0.0.1/png.png">Hello</img>中src上的内容进行下载操作
目前可以使用多种操作方式,例如urlretrieve,不再赘述
Python爬虫框架的更多相关文章
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- Linux 安装python爬虫框架 scrapy
Linux 安装python爬虫框架 scrapy http://scrapy.org/ Scrapy是python最好用的一个爬虫框架.要求: python2.7.x. 1. Ubuntu14.04 ...
- Python爬虫框架Scrapy实例(三)数据存储到MongoDB
Python爬虫框架Scrapy实例(三)数据存储到MongoDB任务目标:爬取豆瓣电影top250,将数据存储到MongoDB中. items.py文件复制代码# -*- coding: utf-8 ...
- Python爬虫框架Scrapy
Scrapy是一个流行的Python爬虫框架, 用途广泛. 使用pip安装scrapy: pip install scrapy scrapy由一下几个主要组件组成: scheduler: 调度器, 决 ...
- 《Python3网络爬虫开发实战》PDF+源代码+《精通Python爬虫框架Scrapy》中英文PDF源代码
下载:https://pan.baidu.com/s/1oejHek3Vmu0ZYvp4w9ZLsw <Python 3网络爬虫开发实战>中文PDF+源代码 下载:https://pan. ...
- Python爬虫框架Scrapy教程(1)—入门
最近实验室的项目中有一个需求是这样的,需要爬取若干个(数目不小)网站发布的文章元数据(标题.时间.正文等).问题是这些网站都很老旧和小众,当然也不可能遵守 Microdata 这类标准.这时候所有网页 ...
- 常见Python爬虫框架你会几个?
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:三名狂客 正文 注意:如果你Python技术学的不够好,可以点击下方链接 ...
- 《精通Python爬虫框架Scrapy》学习资料
<精通Python爬虫框架Scrapy>学习资料 百度网盘:https://pan.baidu.com/s/1ACOYulLLpp9J7Q7src2rVA
- Python爬虫框架Scrapy获得定向打击批量招聘信息
爬虫,就是一个在网上到处或定向抓取数据的程序,当然,这样的说法不够专业,更专业的描写叙述就是.抓取特定站点网页的HTML数据.只是因为一个站点的网页非常多,而我们又不可能事先知道全部网页的URL地址, ...
随机推荐
- go语言中type的几种使用
type是go语法里的重要而且常用的关键字,type绝不只是对应于C/C++中的typedef.搞清楚type的使用,就容易理解go语言中的核心概念struct.interface.函数等的使用.以下 ...
- T-MAX-测试总结
一.项目相关: 作业相关 具体描述 所属班级 2019秋福大软件工程实践Z班 作业要求 团队作业第五次-项目冲刺 作业正文 T-MAX组--测试总结 团队名称 T-MAX小组 作业目标 将团队的项目做 ...
- Column 'status' specified twice
字段写了两次, 检查下sql语句, 删除一个就好了.
- 数据结构---哈希表的C语言实现(闭散列)
原文地址:https://blog.csdn.net/weixin_40331034/article/details/79461705 构造一种存储结构,通过某种函数(hashFunc)使元素的存储位 ...
- 阿里云yii2 composer update 很慢的解决办法
先执行如下语句 composer global require "fxp/composer-asset-plugin:dev-master" 再执行 composer update
- 使用NGINX+LUA实现WAF功能 和nginx 防盗链
使用NGINX+LUA实现WAF功能 一.了解WAF 1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: WAF) ...
- osg::Node clone
深度拷贝 node.clone(osg::CopyOp::DEEP_COPY_ALL) osg::ref_ptr<osg::Node> deepnode = (osg::Node *)( ...
- j2s7s300 refers to jaco v2 7DOF spherical 3fingers
<?xml version="1.0"?> <!-- j2s7s300 refers to jaco v2 7DOF spherical 3fingers --& ...
- Java中字符串操作的基本方法总结:
1.字母大小写转换: package com.imooc; public class SortDemo { public static void main(String[] args) { char ...
- linux记录-docker配置mysql
docker部署mysql 1.拉取镜像 docker pull mysql 2.docker rm containerID 删除镜像iD 3.创建镜像 docker run --name=m ...