玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容

python爬虫抓取图片 终端报错 <urlopen error no hsot given> 是什么原因?

python利用urllib爬虫,图片获取二十几张后就报错
python版本3.6 windows系统下运行

urllib.error.URLError:<urlopen error no host given>

代码如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-
import urllib
import requests
import re
from bs4 import BeautifulSoup
import csv
import socket 
socket.setdefaulttimeout(300)
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}

#url池
url1 = ('http://jandan.net/ooxx/page-')
urls = []
src_list = []
for i in range(2000,2020):
    
    urls.append(url1+str(i))
print (urls)  
for url in urls:#从url池中获取url
    r = requests.get(url,headers = headers)#获取页面
    html = BeautifulSoup(r.text,'html.parser')#利用tag结构划分#获取名称
    
    
    for link in html.find_all('img'):
        src_list.append('http:'+link.get('src'))
                

src_list.pop()
print (src_list)

tmp = 1        
for src in src_list:
    urllib.request.urlretrieve(src,r'C:\Users\姜梦天\Desktop\spider\img\%s.jpg' % tmp)
    tmp += 1
    
print ('下载完毕')


.....尝试解决未果...
求各位大大DEBUG....
#coding=utf-8

import os
import requests
from pyquery import PyQuery as Q

def download_image(image_url):
    if not image_url.startswith('http:'):
        image_url = 'http:{}'.format(image_url)

    image_dir = './image'
    if not os.path.exists(image_dir):
        os.mkdir(image_dir)

    _, image_name = os.path.split(image_url)
    image_path = os.path.join(image_dir, image_name)

    print 'download:', image_url,

    try:
        r = requests.get(image_url)
        with open(image_path, 'wb') as f:
            f.write(r.content)
        print 'success'
    except:
        print 'error'

if __name__ == '__main__':
    for i in range(2000, 2020):
        url = 'http://jandan.net/ooxx/page-{0}'.format(i)
        print 'get:', url
        r = requests.get(url)
        for _ in Q(r.text).find('img'):
            image_url = Q(_).attr('src')
            download_image(image_url)


......改进了代码.写入了try.虽然还是会报错......但是不妨碍继续下载图片........
代码如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-
import urllib
import requests
import re
from bs4 import BeautifulSoup
import csv
import socket 
socket.setdefaulttimeout(300)
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}

#url池
url1 = ('http://jandan.net/ooxx/page-')
urls = []
src_list = []
for i in range(1500,2380):
    
    urls.append(url1+str(i))
print (urls)  
for url in urls:#从url池中获取url
    r = requests.get(url,headers = headers)#获取页面
    html = BeautifulSoup(r.text,'html.parser')
    for link in html.find_all('img'):
        try:
            src_list.append('http:'+link.get('src'))
        except:
            print ('url获取错误')

  
src_list.pop()
print (src_list)

tmp = 0        
for src in src_list:
    try:
        urllib.request.urlretrieve(src,r'C:\Users\姜梦天\Desktop\spider\img\%s.jpg' % tmp)
        tmp = tmp+1
    except:
        print ('失败')
    
print ('下载完毕')

可以下载众多妹子的图片了

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/wenda/wd13779.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月31日 - 14时31分00秒 发布自玩蛇网

我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules