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

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

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

répondre à tous(2)
Ty80
#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)


伊谢尔伦

...Le code a été amélioré. Un essai a été écrit Bien qu'une erreur soit toujours signalée...mais cela n'empêche pas la poursuite du téléchargement des images.....
Le code est le même. suit :

#!/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 ('下载完毕')

Vous pouvez télécharger des photos de nombreuses filles

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal