iOS程序员如何使用python写网路爬虫(一点更新)

原文链接: iOS程序员如何使用python写网路爬虫
以前看到叶孤城写的iOS程序员如何使用python写网路爬虫一文,就写了一个爬虫练练手,
最近发现原文章的用的Beautiful Soup 3 目前已经停止开发,推荐在现在的项目中使用Beautiful Soup 4
爬的网站连接都不能用了,写下我这边修改的一些东西,仅供参考

欢迎大家来吐槽

<pre>
$sudo pip install beautifulsoup4 或
$sudo easy_install beautifulsoup4 </p>
需要注意的是初始化的时候要使用 BeautifulSoup(html, "html.parser")
</pre>

使用的主网站连接:千图网
页面结构布局如下图,主要是<div class='show-area-pic'
然后拿到其中的img即可了

千图网页结构.jpg
具体的一些操作细节可以参考iOS程序员如何使用python写网路爬虫一文,我就不多赘述了,默认是保存在桌面上新建了一个58pic的文件夹里,也可以修改一下存到sqlite里,也很简单的,以后再更新

详细代码如下:
<pre>

--coding:utf-8--

! /usr/bin/python

import math
import urllib
import urllib2
import os

import request

import sqliteManager
import sys
from bs4 import BeautifulSoup;
def getAllImageLink(start,end):
i = start
path = os.path.expanduser(r'~/Desktop/')
folder = '58pic'
fullPath = path + folder
os.chdir(path)
if os.path.exists(fullPath) == False :
os.mkdir(folder)
else:
# print (unicode('已经存在文件夹: rosi','utf-8'))
print ('has exist')
os.chdir(fullPath)
mainPath = os.getcwd()
while (i < end):
os.chdir(mainPath)
url = 'http://www.58pic.com/yuanchuang/%d.html' % i
try:
response=urllib2.urlopen(url,data=None,timeout=120)
# response=.get(url)
except urllib2.URLError as e:
print ('%s : %s' % (url,e.reason))
i += 1
continue
print (url)
html = response.read()
if html.strip()=='':
print ('not exist URL: %s',url)
continue
soup = BeautifulSoup(html, "html.parser")
liResult = soup.findAll('div',attrs={"class":"show-area-pic"})
for li in liResult:
imageEntityArray = li.findAll('img')
for image in imageEntityArray:
title = image.get('title')
href = image.get('src')
splitList = href.split('/')
fileSavePath = mainPath + '/' + str(splitList[len(splitList) - 1])
urllib.urlretrieve(href,fileSavePath)
print (fileSavePath)
i = i + 1
if __name__ == '__main__':
start = 19840500
getAllImageLink(start,start + 100)
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我上一篇blog说过,iOS开发如果之前没接触过除了c和c++(c++太难了,不花个十来年基本不可能精通)的语言,...
    danielss阅读 773评论 1 1
  • 爬虫文章 in 简书程序员专题: like:128-Python 爬取落网音乐 like:127-【图文详解】py...
    喜欢吃栗子阅读 22,072评论 4 411
  • 浮点数存储的是近似值而不是确切的值;所以double的值存在不确定情况在mysql中:使用DECIMAL,在jav...
    白敏鸢阅读 498评论 0 0
  • 玮莲 家乡的母亲河 呻吟着断了弦 没有了旋律 哑然着失了声 河床泛着白光 沙化着荒凉 天也没了色彩 云皱巴巴干瘪着...
    骄阳下的一朵莲阅读 276评论 3 8
  • 在一个阳光明媚的下午,我和我的“秘密花园”相见了。 说是“秘密花园”,但其实它一点也不隐蔽,不像陶渊明笔下的桃花源...
    Emily要加油阅读 1,873评论 0 1