FuluoQued 2018-10-06 13:44 采纳率: 100%
浏览 2716
已采纳

刚接触到爬虫,使用 re.findall()的时候老是抛出这个错误,是正则表达式的错误么?

代码如下:

import urllib.request
import re

def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    return html

def getImg(html):
    reg = r'src="(.+?\.jpg)"pic_ext'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    x = 0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl,'D:\E\%s.jpg' % x)
        x+=1


if __name__ == '__main__':
    html = getHtml("https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%C3%C0%C5%AE&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111")
    print(getImg(html))

抛错是这样的:

   File "E:/SRCS/python/DownPic/GetPic/GetPic.py", line 12, in getImg
    imglist = re.findall(imgre,html)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\re.py", line 222, in findall
    return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
  • 写回答

3条回答 默认 最新

  • qq_43253396 2018-10-07 10:41
    关注

    则 re.findall 的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)语法:

            1
    
    
            findall(pattern, string, flags=0)
    
        import re Python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串 # print (help(re.findall))# print (dir(re.findall)) findall查找全部r标识代表后面是正则的语句
    
    
            1
    
            2
    
            3
    
    
            regular_v1 = re.findall(r"docs","https://docs.python.org/3/whatsnew/3.6.html")
    
            print (regular_v1)
    
            # ['docs']
    

    符号^表示匹配以https开头的的字符串返回,

            1
    
            2
    
            3
    
    
            regular_v2 = re.findall(r"^https","https://docs.python.org/3/whatsnew/3.6.html")
    
            print (regular_v2)
    
            # ['https']
    
          
    

    用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串

            1
    
            2
    
            3
    
    
            regular_v3 = re.findall(r"html$","https://docs.python.org/3/whatsnew/3.6.html")
    
            print (regular_v3)
    
            # ['html']
    

    [...]匹配括号中的其中一个字符

            1
    
            2
    
            3
    
    
            regular_v4 = re.findall(r"[t,w]h","https://docs.python.org/3/whatsnew/3.6.html")
    
            print (regular_v4)
    
            # ['th', 'wh']
    

    “d”是正则语法规则用来匹配0到9之间的数返回列表

            1
    
            2
    
            3
    
            4
    
            5
    
            6
    
    
            regular_v5 = re.findall(r"\d","https://docs.python.org/3/whatsnew/3.6.html")
    
            regular_v6 = re.findall(r"\d\d\d","https://docs.python.org/3/whatsnew/3.6.html/1234")
    
            print (regular_v5)
    
            # ['3', '3', '6']
    
            print (regular_v6)
    
            # ['123']
    

    小d表示取数字0-9,大D表示不要数字,也就是出了数字以外的内容返回

            1
    
            2
    
            3
    
    
            regular_v7 = re.findall(r"\D","https://docs.python.org/3/whatsnew/3.6.html")
    
            print (regular_v7)
    
            # ['h', 't', 't', 'p', 's', ':', '/', '/', 'd', 'o', 'c', 's', '.', 'p', 'y', 't', 'h', 'o', 'n', '.', 'o', 'r', 'g', '/', '/', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '/', '.', '.', 'h', 't', 'm', 'l']
    

    “w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9

            1
    
            2
    
            3
    
    
            regular_v8 = re.findall(r"\w","https://docs.python.org/3/whatsnew/3.6.html")
    
            print (regular_v8)
    
            #['h', 't', 't', 'p', 's', 'd', 'o', 'c', 's', 'p', 'y', 't', 'h', 'o', 'n', 'o', 'r', 'g', '3', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '3', '6', 'h', 't', 'm', 'l']
    

    “W”在正则里面代表匹配除了字母与数字以外的特殊符号

            1
    
            2
    
            3
    
    
            regular_v9 = re.findall(r"\W","https://docs.python.org/3/whatsnew/3.6.html")
    
            print (regular_v9)
    
            # [':', '/', '/', '.', '.', '/', '/', '/', '.',
    

    求采纳缺几个c币谢谢大佬

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算