panda900209 发表于 2017-8-20 16:46:25

跟着小甲鱼视频编写爬虫图片代码,运行后报错400:Bad request?求指导这是什么问题...

爬虫煎蛋图片代码如下:
import urllib.request
import os

def url_open(url):
   
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36')
    response = urllib.request.urlopen(url)
    html = response.read()
   
    return html

def get_page(url):
   
    html = url_open(url).decode('utf-8')

    a = html.find('current-comment-page') + 23
    b = html.find(']',a)
   
    return html

def find_imgs(url):   
    html = url_open(url).decode('utf-8')
    img_addrs = []

    a = html.find('img style="max-height: 750px; max-width: 480px;" src=')#检查元素后,得到的src在style后,这个如何处理?
    while a != -1:
      b = html.find('.jpg',a,a+255)
      if b != -1:
            img_addrs.append(html)
      else:
            b = a + 53
      
      a = html.find('img style="max-height: 750px; max-width: 480px;" src=',b)
      
    return img_addrs

def save_imgs(folder,img_addrs):
    for each in img_addrs:
      filename = each.split('/')[-1]
      with open(filename,'wb') as f:
            img = open_url(each)
            f.write(img)

def download_mm(folder = 'mm',pages = 5):
    os.mkdir(folder)
    os.chdir(folder)

    url = "http://jandan.net/ooxx/"
    page_num = int(get_page(url))

    for i in range(pages):
      page_num -= i
      page_url = url + 'page- ' + str(page_num) + '#comments'
      img_addrs = find_imgs(page_url)
      save_imgs(folder,img_addrs)

if __name__ == '__main__':
    download_mm()

运行后报出来的错误如下:


求指导,这是啥问题。谢谢啦

SongQing 发表于 2019-1-23 20:49:18

+++++++++++++++++++++++++++1
试了好多个代理ip了,要么是400 Bad Request,要么就是连接时间过长,或者直接被禁止了{:10_277:}
页: [1]
查看完整版本: 跟着小甲鱼视频编写爬虫图片代码,运行后报错400:Bad request?求指导这是什么问题...