python之网络爬虫

简介:

一、演绎自已的北爱 


        踏上北漂的航班,开始演奏了我自已的北京爱情故事


二、爬虫1

1、网络爬虫的思路

首先:指定一个url,然后打开这个url地址,读其中的内容。

其次:从读取的内容中过滤关键字;这一步是关键,可以通过查看源代码的方式获取。

最后:下载获取的html的url地址,或者图片的url地址保存到本地


2、针对指定的url来网络爬虫

分析:

第一步:大约共有4300个下一页。

第二步:一个页面上有10个个人头像

第三步:一个头像内大约有100张左右的个人图片

指定的淘宝mm的url为:http://mm.taobao.com/json/request_top_list.htm?type=0&page=1

wKiom1Re1DjxFLWEAAI4xhSpEeU805.jpg

这个页面默认是没有下一页按钮的,我们可以通过修改其url地址来进行查看下一个页面

wKioL1Re1WrieaAHAAJOq58aESc130.jpg

最后一页的url地址和页面展示如下图所示:

wKiom1Re1W-xRcNPAAEzpyPjmyo284.jpg

点击任意一个头像来进入个人的主页,如下图

wKioL1Re1q7AU3EwAANbsDDzi1k438.jpg

3、定制的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
#coding:utf-8
#Author:Allentuns
#Email:zhengyansheng@hytyi.com
 
 
import  urllib
import  os
import  sys
import  time
 
ahref =  '<a href="'
ahrefs =  '<a href="h'
ahtml =  ".htm"
atitle =  "<img style"
ajpg =  ".jpg"
btitle =  '<img src="'
 
page = 0
while  page < 4300:     #这个地方可以修改;最大值为4300,我测试的时候写的是3.
         mmurl =  "http://mm.taobao.com/json/request_top_list.htm?type=0&page=%d"  %(page)
         content = urllib.urlopen(mmurl). read ()
 
         href = content. find (ahref)
         html = content. find (ahtml)
         url = content[href + len(ahref) : html + len(ahtml)]
         print url
         imgtitle = content. find (btitle,html)
         imgjpg = content. find (ajpg,imgtitle)
         littleimgurl = content[imgtitle + len(btitle): imgjpg + len(ajpg)]
         print littleimgurl
 
         urllib.urlretrieve(littleimgurl, "/www/src/temp/image/taobaomm/allentuns.jpg" )
 
         s = 0
         while  s < 18:
                 href = content. find (ahrefs,html)
                 html = content. find (ahtml,href)
                 url = content[href + len(ahref): html + len(ajpg)]
                 print s,url
 
                 imgtitle = content. find (btitle,html)
                 imgjpg = content. find (ajpg,imgtitle)
                 littleimgurl = content[imgtitle : imgjpg + len(ajpg)]
                 littlesrc = littleimgurl. find ( "src" )
                 tureimgurl = littleimgurl[littlesrc + 5:]
                 print s,tureimgurl
 
 
                 if  url. find ( "photo" ) == -1:
                         content01 = urllib.urlopen(url). read ()
                         imgtitle = content01. find (atitle)
                         imgjpg = content01. find (ajpg,imgtitle)
                         littleimgurl = content01[imgtitle : imgjpg + len(ajpg)]
                         littlesrc = littleimgurl. find ( "src" )
                         tureimgurl = littleimgurl[littlesrc + 5:]
                         print tureimgurl
 
                         imgcount = content01.count(atitle)
                         i = 20
                         try:
                                 while  i < imgcount:
                                         content01 = urllib.urlopen(url). read ()
                                         imgtitle = content01. find (atitle,imgjpg)
                                         imgjpg = content01. find (ajpg,imgtitle)
                                         littleimgurl = content01[imgtitle : imgjpg + len(ajpg)]
                                         littlesrc = littleimgurl. find ( "src" )
                                         tureimgurl = littleimgurl[littlesrc + 5:]
                                         print i,tureimgurl
                                         time . sleep (1)
                                         if  tureimgurl.count( "<" ) == 0:
                                                 imgname = tureimgurl[tureimgurl.index( "T" ):]
                                                 urllib.urlretrieve(tureimgurl, "/www/src/temp/image/taobaomm/%s-%s"  %(page,imgname))
                                         else :
                                                 pass
                                         i += 1
                         except IOError:
                                 print  '/nWhy did you do an EOF on me?'
                                 break
                         except:
                                 print  '/nSome error/exception occurred.'
 
                 s += 1
         else :
                 print  "---------------{< 20;1 page hava 10 htm and pic  }-------------------------}"
         page = page + 1
         print  "****************%s page*******************************"  %(page)
else :
         print  "Download Finshed."

4、图片展示(部分图片)

wKioL1Re2ICiL6AQAAhVHpRxlWc887.jpg

wKiom1Re2BvxeUXWAAhjd5UBBfo108.jpg

5、查看下载的图片数量

wKioL1Re2QvDnZb3AAIXXHul8HU186.jpgwKiom1Re2MrAMoPXAAtfpHgkZHU553.jpg


二、爬虫2

1、首先来分析url

第一步:总共有7个页面;

第二步:每个页面有20篇文章

第三步:查看后总共有317篇文章

wKiom1Re2xzwiidxAAZjxM8KAOM147.jpg

wKioL1Re24aQ2iNbAAd97-WqAm4390.jpg

wKiom1Re2yGwofzrAAeL33Kq3ao239.jpg

2、python脚本

脚本的功能:通过给定的url来将这片博客里面的所有文章下载到本地

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
#coding: utf-8
 
import  urllib
import  time
 
list00 = []
i = j = 0
page = 1
 
while  page < 8:
         str =  "http://blog.sina.com.cn/s/articlelist_1191258123_0_%d.html"  %(page)
         content = urllib.urlopen(str). read ()
 
         title = content. find (r "<a title" )
         href  = content. find (r "href=" ,title)
         html  = content. find (r ".html" ,href)
         url = content[href + 6:html + 5]
         urlfilename = url[-26:]
         list00.append(url)
         print i,  url
 
         while  title != -1 and href != -1 and html != -1 and i < 350:
                 title = content. find (r "<a title" ,html)
                 href  = content. find (r "href=" ,title)
                 html  = content. find (r ".html" ,href)
                 url = content[href + 6:html + 5]
                 urlfilename = url[-26:]
                 list00.append(url)
                 i = i + 1
                 print i,  url
         else :
                 print  "Link address Finshed."
 
         print  "This is %s page"  %(page)
         page = page + 1
else :
         print  "spage=" ,list00[50]
         print list00[:51]
         print list00.count( "" )
         print  "All links address Finshed."
 
x = list00.count( '' )
a = 0
while  a < x:
         y1 = list00.index( '' )
         list00.pop(y1)
         print a
         a = a + 1
 
print list00.count( '' )
listcount = len(list00)
 
 
while  j < listcount:
         content = urllib.urlopen(list00[j]). read ()
         open (r "/tmp/hanhan/" +list00[j][-26:], 'a+' ).write(content)
         print  "%2s is finshed."  %(j)
         j = j + 1
         #time.sleep(1)
else :
         print  "Write to file End."

3、下载文章后的截图

wKioL1Re3R3gg6IGAA8dyZrP0U4461.jpg4、从linux下载到windows本地,然后打开查看;如下截图

wKiom1Re3XzBVDbjAAlH2M5t-kI943.jpg












     本文转自zys467754239 51CTO博客,原文链接:http://blog.51cto.com/467754239/1574528,如需转载请自行联系原作者











相关文章
|
11天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
12天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
12天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
15天前
|
数据采集 Python
【python】爬虫-西安医学院-校长信箱
本文以西安医学院-校长信箱为基础来展示爬虫案例。来介绍python爬虫。
【python】爬虫-西安医学院-校长信箱
|
21天前
|
数据采集 安全 Python
python并发编程:Python实现生产者消费者爬虫
python并发编程:Python实现生产者消费者爬虫
24 0
python并发编程:Python实现生产者消费者爬虫
|
1天前
|
数据采集 存储 JSON
Python爬虫面试:requests、BeautifulSoup与Scrapy详解
【4月更文挑战第19天】本文聚焦于Python爬虫面试中的核心库——requests、BeautifulSoup和Scrapy。讲解了它们的常见问题、易错点及应对策略。对于requests,强调了异常处理、代理设置和请求重试;BeautifulSoup部分提到选择器使用、动态内容处理和解析效率优化;而Scrapy则关注项目架构、数据存储和分布式爬虫。通过实例代码,帮助读者深化理解并提升面试表现。
10 0
|
4天前
|
数据采集 JavaScript 前端开发
使用Python打造爬虫程序之破茧而出:Python爬虫遭遇反爬虫机制及应对策略
【4月更文挑战第19天】本文探讨了Python爬虫应对反爬虫机制的策略。常见的反爬虫机制包括User-Agent检测、IP限制、动态加载内容、验证码验证和Cookie跟踪。应对策略包括设置合理User-Agent、使用代理IP、处理动态加载内容、验证码识别及维护Cookie。此外,还提到高级策略如降低请求频率、模拟人类行为、分布式爬虫和学习网站规则。开发者需不断学习新策略,同时遵守规则和法律法规,确保爬虫的稳定性和合法性。
|
5天前
|
机器学习/深度学习 Python
Python用LSTM长短期记忆神经网络对不稳定降雨量时间序列进行预测分析
Python用LSTM长短期记忆神经网络对不稳定降雨量时间序列进行预测分析
16 0
|
5天前
|
JSON 网络协议 API
Python网络编程面试题精讲
【4月更文挑战第15天】本文介绍了Python网络编程的面试重点,包括基础Socket编程、HTTP协议与requests库、异步编程与asyncio库。通过实例解析常见面试题,强调了非阻塞套接字、异常处理、HTTP状态码检查以及异步任务管理等关键点。提供代码示例帮助读者巩固概念,助力面试准备。
11 0
|
7天前
|
机器学习/深度学习 存储 测试技术
使用PYTHON中KERAS的LSTM递归神经网络进行时间序列预测
使用PYTHON中KERAS的LSTM递归神经网络进行时间序列预测
18 0