python编写爬虫获取区域代码-递归获取所有子页面
2017-07-03 15:33
691 查看
上一篇文章用htmlparser写了一个java的获取区域的爬虫,觉得太笨重。发现python也可以实现这个功能。
这里就简单写一个用python3写的小爬虫例子
功能目标:对指定网站的所有区域信息进行筛选,并保存到文本中
思路:1、定义一个队列,初始向队列中put一个地址
2、判断队列是否为空,不为空调用getURL函数,为空则结束
3、getURL获取URL链接的内容,并用beautifulSoup(第三方需要单独安装,可百度)匹配a链接
4、对匹配的内容进行字符串拼接,调用text_create保存成文本
声明:这个只是一个DEMO,可以参考学习使用,所以逻辑不是很严谨,大家勿喷。
#coding:utf-8
import urllib.request
import sys,operator
import queue
import re
import sys
from bs4 import BeautifulSoup
base = "http://www.diqudaima.com/"
lr = queue.Queue()
def text_create(name, msg):#保存文件,传入文件名与内容
desktop_path = '/Users/wuhao/Desktop/python/'
full_path = desktop_path + name + '.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
print('Done :',name)
def getURL(url):
try:
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
the_page = response.read().decode("GBK")
soup = BeautifulSoup(the_page, 'html.parser')
data=soup.select("div ul li a")#获取a链接
body =''
if len(data)>0:
for html_tag in data:
print("title : "+html_tag.string)
print(base+html_tag['href'][1:])
body+=html_tag.string
body+='\n'
lr.put_nowait(base+html_tag['href'][1:])
else:
data=soup.select("div ul li")
for html_tag in data:
print("title : "+html_tag.string)
body+=html_tag.string
body+='\n'
mode = re.compile(r'\d+')
print (mode.findall(url))
if len(mode.findall(url))>0:
text_create(mode.findall(url)[0],body)
except:#因为可能出现TimeoutError等异常,这里捕获并重新放入队列中
print("链接超时,未处理链接:",lr.qsize())
lr.put_nowait(url)
else:
print("未处理链接:",lr.qsize())
if __name__ == "__main__":
url="http://www.diqudaima.com/zhejiang/hangzhoushi/"
lr.put_nowait(url)
while not lr.empty():
vistorUrl =lr.get_nowait()
print ("访问链接:"+vistorUrl)
getURL(vistorUrl)
运行结果:
这里就简单写一个用python3写的小爬虫例子
功能目标:对指定网站的所有区域信息进行筛选,并保存到文本中
思路:1、定义一个队列,初始向队列中put一个地址
2、判断队列是否为空,不为空调用getURL函数,为空则结束
3、getURL获取URL链接的内容,并用beautifulSoup(第三方需要单独安装,可百度)匹配a链接
4、对匹配的内容进行字符串拼接,调用text_create保存成文本
声明:这个只是一个DEMO,可以参考学习使用,所以逻辑不是很严谨,大家勿喷。
#coding:utf-8
import urllib.request
import sys,operator
import queue
import re
import sys
from bs4 import BeautifulSoup
base = "http://www.diqudaima.com/"
lr = queue.Queue()
def text_create(name, msg):#保存文件,传入文件名与内容
desktop_path = '/Users/wuhao/Desktop/python/'
full_path = desktop_path + name + '.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
print('Done :',name)
def getURL(url):
try:
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
the_page = response.read().decode("GBK")
soup = BeautifulSoup(the_page, 'html.parser')
data=soup.select("div ul li a")#获取a链接
body =''
if len(data)>0:
for html_tag in data:
print("title : "+html_tag.string)
print(base+html_tag['href'][1:])
body+=html_tag.string
body+='\n'
lr.put_nowait(base+html_tag['href'][1:])
else:
data=soup.select("div ul li")
for html_tag in data:
print("title : "+html_tag.string)
body+=html_tag.string
body+='\n'
mode = re.compile(r'\d+')
print (mode.findall(url))
if len(mode.findall(url))>0:
text_create(mode.findall(url)[0],body)
except:#因为可能出现TimeoutError等异常,这里捕获并重新放入队列中
print("链接超时,未处理链接:",lr.qsize())
lr.put_nowait(url)
else:
print("未处理链接:",lr.qsize())
if __name__ == "__main__":
url="http://www.diqudaima.com/zhejiang/hangzhoushi/"
lr.put_nowait(url)
while not lr.empty():
vistorUrl =lr.get_nowait()
print ("访问链接:"+vistorUrl)
getURL(vistorUrl)
运行结果:
相关文章推荐
- Python get获取页面cookie代码实例
- python简易爬虫编写--图片获取
- 从零开始编写简易python爬虫:每日房价的获取
- 编写Python代码——爬取百度百科Python词条相关1000个页面数据【未完慕课】
- python爬虫-第一步,获取页面
- selenium自动化测试工具开发python爬虫-动态加载页面数据获取
- 使用python爬虫获取黄金价格的核心代码
- 【爬虫】Python2爬虫代码之获取金融品种行情数据
- Python编写网页爬虫爬取oj上的代码信息
- Python新手写出漂亮的爬虫代码2——从json获取信息
- Python爬虫入门之一(获取页面响应)
- Python 自用代码(scrapy多级页面(三级页面)爬虫)
- Python编写网页爬虫爬取oj上的代码信息
- python 爬虫 批量获取代理ip的实例代码
- 零基础掌握百度地图兴趣点获取POI爬虫(python语言爬取)(代码篇)
- Python新手写出漂亮的爬虫代码2——从json获取信息
- Python 简易网络爬虫的编写——抓取任意页面数目百度百科内容
- 爬虫技术(六)-- 使用HtmlAgilityPack获取页面链接(附c#代码及插件下载)
- 使用eclipse+python编写爬虫获取python百科的1000条词条
- 优酷电视剧爬虫代码实现一:下载解析视频网站页面(3)补充知识点:怎样获取XPath