Pyton erro“AttributeError模块'string'没有属性'lstrip'”

2024-04-25 06:10:30 发布

您现在位置:Python中文网/ 问答频道 /正文

启动此代码时,我无法保存在网站上找到的文件。我得到一个错误“AttributeError模块'string'没有属性'lstrip'” 我用“string.lstpri”解决不了问题,帮我找错了吗

from bs4 import BeautifulSoup
import mechanize
import time
import urllib.request
import string


start = "http://www.irrelevantcheetah.com/browserimages.html"# test website 
filetype = input("What file type are you looking for?\n")

br = mechanize.Browser()
r = br.open(start)
html = r.read()

soup = BeautifulSoup(html, "html5lib")
for link in soup.find_all('a'):
    linkText = str(link)
    fileName = str(link.get('href'))
    if filetype in fileName:
       image = urllib.request.URLopener()
       linkGet = "http://www.irrelevantcheetah.com" + fileName
       filesave = string.lstrip(fileName, '/')
       image.retrieve(linkGet, filesave)

Tags: importcomhttpstringrequesthtmlwwwlink
1条回答
网友
1楼 · 发布于 2024-04-25 06:10:30

Andrej Kesely gave the solution in a comment

filesave = string.lstrip(fileName, '/') should be filesave = fileName.lstrip('/')string is name of module you`ve imported before

相关问题 更多 >