여러 줄 목록에서 wget을 파일 이름으로 변환하는 방법은 무엇입니까?

여러 줄 목록에서 wget을 파일 이름으로 변환하는 방법은 무엇입니까?

XML 파일에서 검색된 항목 목록을 가져오고 싶습니다. sed를 사용하여 XML을 정리한 결과 다음과 같은 결과가 나왔습니다.

CountofMonteCristo.zip
English.
http://www.archive.org/download/count_monte_cristo_0711_librivox/count_monte_cristo_0711_librivox_64kb_mp3.zip
Alexandre.
Dumas.
LettersofTwoBrides.zip
English.
http://www.archive.org/download/letters_brides_0709_librivox/letters_brides_0709_librivox_64kb_mp3.zip
Honoréde.
Balzac.
BleakHouse.zip
English.
http://www.archive.org/download/bleak_house_cl_librivox/bleak_house_cl_librivox_64kb_mp3.zip
Charles.
Dickens.

wget -i를 사용하여 이 파일을 Language.Lastname.Firstname.Title.zip으로 다운로드하고 싶습니다.

$filename $url을 사용할 수 있도록 어떤 방식으로든 파일을 다시 정렬하겠습니다.

몇 가지 다른 sed 명령을 시도했습니다. XML 태그를 정리하기 위해 Sed를 사용하고 있지만 텍스트를 적절한 위치로 이동하는 방법을 모르겠습니다. 각 파일은 서로 다른 제목, 이름 및 언어를 갖습니다.

편집: sed로 태그를 정리하기 전에 각 줄은 English 및 FileTitle과 같은 태그로 래핑됩니다. 나는 사물을 재배치할 때 패턴을 인식하는 것이 도움이 될 것이라고 생각했습니다.

편집 2: 이것은XML 소스

편집 3: 몇 가지이와 같이작동할 것 같지만 필요에 맞게 수정하는 데 어려움을 겪고 있습니다.

내 최종 목표는 모든 파일을 Language -> AuthorLastnameFirstname -> Files.zip 계층 구조의 폴더로 구성하는 것입니다.

내가 하고 있는 일이 모범 사례가 아닌 경우 대안을 모색할 수 있습니다.

감사해요

답변1

내가 하고 있는 일이 모범 사례가 아닌 경우 대안을 모색할 수 있습니다.

사용하지 않거나 기다리지 않는 것이 bash좋습니다 sed! 그리고 Python 방식을 사용하면 이는 구문 분석해야 하는 XML을 구문 분석하는 더 좋은 방법입니다. 방금 python3.6으로 작성하고 테스트했는데 정확히 원하는 대로 작동합니다.

#!/usr/bin/python3
# Let's import the modules we need
import wget
import os
import requests
from bs4 import BeautifulSoup as bs

# Assign the url to a variable (not essential as we 
# only use it once, but it's pythonic)
url = 'https://librivox.org/api/feed/audiobooks/?offset=0&limit=3&fields=%7Blanguage,authors,title,url_zip_file%7B'

# Use requests to fetch the raw xml
r = requests.get(url)

# Use BeautifulSoup and lxml to parse the raw xml so 
# we can do stuff with it
s = bs(r.text, 'lxml')

# We need to find the data we need. This will find it and create some 
# python lists for us to loop through later

# Find all xml tags named 'url_zip_file' and assign them to variable
links = s.find_all('url_zip_file')

# Find all xml tags named 'last_name' and assign them to variable
last_names = s.find_all('last_name')

# Find all xml tags named 'last_name' and assign them to variable
first_names = s.find_all('first_name')

# Find all xml tags named 'language' and assign them to variable
language = s.find_all('language')

# Assign the language to a variable
english = language[0].text

# Make our new language directory
os.mkdir(english)

# cd into our new language directory
os.chdir(str(english))

# Loop through the last names (ln), first names(fn) and links
# so we can make the directories, download the file, rename the 
# file then we go back a directory and loop again
for ln, fn, link in zip(last_names, first_names, links):
    os.mkdir('Author{}{}'.format(str(ln.text), str(fn.text)))
    os.chdir('Author{}{}'.format(ln.text, fn.text))
    filename = wget.download(link.text)
    os.rename(filename, 'File.zip')
    os.chdir('../')

파일에 저장하거나 python3 인터프리터 cli에 붙여넣거나 입력할 수 있습니다. 선택은 귀하에게 달려 있습니다.

당신은 설치해야합니다python3-wget그리고아름다운 수프 4pip 또는 easy_install 등을 사용하십시오.

답변2

Librivox API는 사용할 수 있는 경우 JSON 출력도 제공하며, 적절한 XML 도구를 사용하여 jqXML을 구문 분석하는 것보다 JSON을 구문 분석하는 것이 더 쉽습니다 .jq

u='https://librivox.org/api/feed/audiobooks/?offset=0&limit=3&fields=%7Blanguage,authors,title,url_zip_file%7B&format=json'
curl "$u" -sL |
  jq -r '.books[] | "\(.language).\(.authors[0].last_name + .authors[0].first_name).\(.title).zip", .url_zip_file'

다음과 같은 출력을 제공합니다.

English.DumasAlexandre.Count of Monte Cristo.zip
http://www.archive.org/download/count_monte_cristo_0711_librivox/count_monte_cristo_0711_librivox_64kb_mp3.zip
English.BalzacHonoré de.Letters of Two Brides.zip
http://www.archive.org/download/letters_brides_0709_librivox/letters_brides_0709_librivox_64kb_mp3.zip
English.DickensCharles.Bleak House.zip
http://www.archive.org/download/bleak_house_cl_librivox/bleak_house_cl_librivox_64kb_mp3.zip

그 후에는 사용하기가 더 쉬워집니다 xargs.

curl "$u" -sL |
  jq -r '.books[] | "\(.language).\(.authors[0].last_name + .authors[0].first_name).\(.title).zip", .url_zip_file' |
  xargs -d '\n' -n2 wget -O

xargs두 줄이 매개변수로 사용 되는데 wget첫 번째 줄은 -O옵션 매개변수, 두 번째 줄은 URL이 됩니다.

추천하고 싶지만Jamie와 같은 Python 기반 솔루션단, bs4 대신 JSON 및 Python의 내장 JSON 기능을 사용하는 것은 제외됩니다.

답변3

무차별적인 힘.

구문 분석된 XML이 다음 위치에 있는 경우books

while read a; read b; read c; read d; read e; do wget $c -O $b$e$d$a; echo $c; done < books

행을 변수로 다시 그룹화하고 레코드 블록을 5개 행으로 채우면 됩니다.

관련 정보