a1437485261 发表于 2020-6-22 15:42:19

大佬们为啥0基础python里面正则表达式爬贴吧下载图片的现在已经用不了了

import urllib.request
import re
import os

def open_url(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/83.0.4103.106 Safari/537.36')
    page = urllib.request.urlopen(req)
    html = page.read().decode('utf-8')

    return html

def get_img(html):
    p = r'<img class="BDE_Image" src=("[^"]+\.jpg")'
    imglist = re.findall(p, html)
    '''
    for each in imglist:
      print(each)
    '''

    try:
      os.mkdir('NewPics')
    except FileExistsError:
      # 如果文件夹已存在则覆盖保存!
      pass

    os.chdir('NewPics')
   
    for each in imglist:
      filename = each.split("/")[-1]
      urllib.request.urlretrieve(each, filename, None)

if __name__ == '__main__':
    url = 'https://tieba.baidu.com/p/6747778991'
    get_img(open_url(url))


这个是代码

a1437485261 发表于 2020-6-22 15:43:01

Traceback (most recent call last):
File "I:\Python\爬取贴吧.py", line 35, in <module>
    get_img(open_url(url))
File "I:\Python\爬取贴吧.py", line 31, in get_img
    urllib.request.urlretrieve(each, filename, None)
File "I:\Python\lib\urllib\request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
File "I:\Python\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
File "I:\Python\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
File "I:\Python\lib\urllib\request.py", line 548, in _open
    'unknown_open', req)
File "I:\Python\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
File "I:\Python\lib\urllib\request.py", line 1387, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: "http>

运行之后提示这个

a1437485261 发表于 2020-6-22 15:58:20

有人吗

suchocolate 发表于 2020-6-22 19:15:17

p = r'<img class="BDE_Image" src="(.*?\.jpg)"'
页: [1]
查看完整版本: 大佬们为啥0基础python里面正则表达式爬贴吧下载图片的现在已经用不了了