低調的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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建