低調的D吖 2020-03-04 22:14 采纳率: 0%
浏览 1376

python3 爬照片報錯TypeError: expected string or bytes-like object

#!/usr/local/bin/python3.7
import urllib.request as urlre
import re

def open_url(url):
  req = urlre.Request(url)
  req.add_header('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36')
  page = urlre.urlopen(req)
  html = page.read().decode('utf-8')

  print(type(html)) 

  return html


def get_img(html):
  p = r'src="(.+?\.jpg)" pic_ex'
  imgsa = re.compile(p)
  imglist = re.findall(imgsa,html)

  return imglist

  for each in imglist:
    print(each)

  x = 0
  for each in imglist:
    urlre.urlretrieve(each,':\\%s.jpg' % x)
    x += 1




if __name__ == "__main__":
  url ='http://tieba.baidu.com/p/2460150866'
  get_img(open_url)

return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object

  • 写回答

1条回答 默认 最新

  • 放风喽 2020-03-05 10:39
    关注

    翻译一下
    TypeError: expected string or bytes-like object
    需要字符串或类似字节的对象

    if name == "__main__":
    url ='http://tieba.baidu.com/p/2460150866'
    get_img(open_url)

    open_url需要的参数呢???

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用