python爬虫(爬取蜂鸟网高像素图片)_空网页,错误处理

__author__ = 'AllenMinD'
import requests,urllib,os
from bs4 import  BeautifulSoup

ans = 1 #counting

for page in range(0,43):
    flag = 1 #web exists or not
    if page<10:
        url = 'http://bbs.fengniao.com/forum/pic/slide_101_8903443_8017670'+str(page)+'.html'
    else:
        url = 'http://bbs.fengniao.com/forum/pic/slide_101_8903443_801767'+str(page)+'.html'
    source_code = requests.get(url)
    plain_text = source_code.text

    soup = BeautifulSoup(plain_text,'lxml')

    file_name = ''
    download_link = []
    for pic_tag in soup.find_all('a'):
        if pic_tag.get('href') == '/forum/8903443.html':
            file_name  = pic_tag.get('title')
        if pic_tag.get('class') == ['pictureDownload']:
            if pic_tag.get('href') == '': #if this page is None
                flag = 0
                break
            else:
                download_link.append(pic_tag.get('href'))

    if flag == 0 : #this page is None
        continue

    folder_path = 'D:/spider_things/2016.4.8/' + file_name + '/'

    if not os.path.exists(folder_path):
        os.makedirs(folder_path)

    for item in download_link:
        try:
            urllib.urlretrieve(item,folder_path + str(ans) + '.jpg')
            print 'you have downloaded' , ans , 'pic(s)'
            ans = ans + 1
        except urllib.ContentTooShortError,e: #if the picture is too big , pass it
            continue


这次同样是爬去蜂鸟网的图片,但是中途遇到了2个新问题:

1. 空网页:

蜂鸟网的有些图片集的图片连接不是连号的,这时候就要用一个if语句来跳过一些没有图片的连接

if pic_tag.get('href') == '': #if this page is None
                flag = 0
                break
.....

if flag == 0 : #this page is None
        continue

2.错误处理

爬取这次图片的时候发现,有些图片太大了,超出了urllib.urlretrieve方法所规定的范围,即出现报错:urllib.ContentTooShortError

这时候,要利用try...except 来处理

try...except的格式是:

try:
    ......
except 错误类型(如urllib.ContentTooShortError),e:
    ......


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Python爬虫爬取网页数据并解析数据的教程: 1. 确定目标网站和要爬取的信息 首先,需要确定要爬取的网站和要提取的信息。可以使用Python的requests库向网站发送HTTP请求获取HTML源代码,并使用BeautifulSoup库解析HTML文档获取目标数据。 例如,我们要爬取CSDN博客的文章标题和链接,可以先打开CSDN博客主页,右键查看网页源代码,找到文章标题和链接所在的HTML标签。 2. 发送HTTP请求获取HTML源代码 接下来,使用Python的requests库向网站发送HTTP请求,获取HTML源代码。 ``` import requests url = 'https://blog.csdn.net/' response = requests.get(url) html = response.text ``` 3. 解析HTML文档获取目标数据 使用BeautifulSoup库解析HTML文档,获取目标数据。 ``` from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'html.parser') titles = soup.find_all('div', class_='title') for title in titles: link = title.find('a').get('href') title_text = title.find('a').text.strip() print(title_text, link) ``` 上述代码中,通过`find_all`方法找到所有class属性为"title"的div标签,然后在每个div标签中找到第一个a标签,获取链接和标题文本。 4. 完整代码 ``` import requests from bs4 import BeautifulSoup url = 'https://blog.csdn.net/' response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') titles = soup.find_all('div', class_='title') for title in titles: link = title.find('a').get('href') title_text = title.find('a').text.strip() print(title_text, link) ``` 以上就是一个简单的Python爬虫爬取网页数据并解析数据的教程。需要注意的是,在爬取网站数据时要遵守网站的爬虫协议,避免被网站封禁IP。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值