2017-10-13 1 views
-1

현재 사이트를 긁어서 mp3 파일을 다운로드하는 스크립트를 작성 중이며 BeautifulSoupPython 3.6.3을 사용하여 개별 아티스트 페이지로 연결되는 모든 링크를 가져옵니다. 그러나 일부 구속 된 URL에는 둘 이상의 "." 그것에 불이 났을 때Python requests.get() url에 여러 개의 "."

request.get(url, header=<random header using fake_useragent>) 

파일을 다운로드하지 않습니다. 어떻게 수정합니까?

URL > abc.com/mp3/artist/songs/song.com.mp3 (not downloading) 
URL > abc.com/mp3/artist/songs/song.mp3 (downloading) 

코드 : : "."

def download_mp3(url_list_file, download_dir):                                         
    with open(url_list_file, 'r', encoding='utf-8') as urls:                                      

     for url in urls:                                               
      ua = fake_useragent.UserAgent(verify_ssl=False)                                      
      #header = {'User-Agent': str(ua.chrome.random)}                                      
      artist_dir = url.split('/')[4]                                          
      song_name = url.split('/')[6].replace('\n', '')                                      
      #corrected_url = ('path:url')                                           
      #print(corrected_url)                                             
      download_to = os.path.join(download_dir, artist_dir, song_name)                                  
      save_path = os.path.join(download_dir, artist_dir)                                     
      #print(download_to)                                             
      #print(save_path)                                              
      print(url)                                               
      if(os.path.isdir(save_path) == True):                                         
       #print ('True')                                             
       header = {'User-Agent': str(ua.random)}                                       
       lower_download_to = str.lower(song_name) 
       #To get correct file name 
       mp3_file_name = os.path.join(download_dir, artist_dir, lower_download_to.replace("www.", "").replace("[", "").replace("]", "")) 
       mp3_file = open(mp3_file_name, "wb")                                        
       temp_file = requests.get(url, headers=header)                                   
       mp3_file.write(temp_file.content)                                         
       time.sleep(random.randint(5,10))                                         
      else:                                                 
       #print('False') 
       #To create Artist folder names 
       os.mkdir(save_path)                                            
       header = {'User-Agent': str(ua.random)}                                       
       lower_download_to = str.lower(download_to) 
       #To get correct file name    
       mp3_file_name = os.path.join(download_dir, artist_dir, lower_download_to.replace("www.", "").replace("[", "").replace("]", "")) 
       mp3_file = open(mp3_file_name, "wb")                                        
       temp_file = requests.get(url, headers=header)                                      
       mp3_file.write(temp_file.content)                                         
       time.sleep(random.randint(5,10))                                         
    urls.closed 
+1

'requests'는 해당 수준의 URL 구조에 신경을 쓰지 않습니다. 문제를 보여주는 실제적이고 실행 가능한 코드를 게시 할 수 있습니까? 그렇게하면 우리가 쉽게 도울 수 있습니다. – larsks

+0

안녕하세요 larsks, 질문을 업데이트했습니다 – Chamisxs

+1

"다운로드 안함"이라고 말하면 오류가 있습니까? 어떤 오류입니까? –

답변

0

그것이 모든 대체 보정 가지고 예를 들어

사용하려면 string.replace 퍼센트 인코딩과 ".MP3"의 마지막을 제외한 URL에 ('.', '%의 2E」(string.count()을'. '- 1))

관련 문제