网页爬虫 - python爬虫爬图,报错<urlopen error no hsot given>.
黄舟
黄舟 2017-04-18 10:24:51
0
2
1187

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....
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回覆(2)
Ty80

雷雷

伊谢尔伦

......改進了代碼.寫入了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 ('下载完毕')

可以下載眾多妹子的圖片了

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板