eavv 发表于 2017-1-4 21:26:34

有关爬虫的问题

import urllib.request
import re

def open_url(url):
    req=urllib.request.Request(url)
    req.add_header('user-agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36')
    page=urllib.request.urlopen(req)
    html=page.read().decode('utf-8')
    return html
def get_img(html):
    p=r'<img src="http://([^"]+\.jpg)'
    imagelist=re.findall(p,html)#应用findall方法中,如果给出的re有子组,那么就会把子组的内容单个返回回来
                              #如果存在多个子组,将匹配的内容组合成元组 再返回

    for each in imagelist:
      print(each)
      # filename=each.split("/")[-1]
      # urllib.request.urlretrieve(each,filename,None)




if __name__=='__main__':
    url="http://tieba.baidu.com/p/2354064447#!/l/p1"
    get_img(open_url(url))

是这样的,我这样运行的时候程序是可以打印出字符串的
然而,当我调用urllib.request.urlretrieve(each,filename,None)方法后(也就是打印语句下面的两行)后,它就报错
错误信息:
C:\Users\qx\AppData\Local\Programs\Python\Python35-32\python.exe "F:/work place/pycharm/爬虫/get_img.py"
Traceback (most recent call last):
File "F:/work place/pycharm/爬虫/get_img.py", line 25, in <module>
    get_img(open_url(url))
File "F:/work place/pycharm/爬虫/get_img.py", line 18, in get_img
    urllib.request.urlretrieve(each,filename,None)
File "C:\Users\qx\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 188, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\qx\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
File "C:\Users\qx\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 451, in open
    req = Request(fullurl, data)
File "C:\Users\qx\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 269, in __init__
    self.full_url = url
File "C:\Users\qx\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 295, in full_url
    self._parse()
File "C:\Users\qx\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 324, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'imgsrc.baidu.com/forum/pic/item/bede9735e5dde711c981db20a0efce1b9f1661d5.jpg'

Process finished with exit code 1
页: [1]
查看完整版本: 有关爬虫的问题