curlコマンドのURLを繰り返すには?

curlコマンドのURLを繰り返すには?

私はWebスクレイピング(および一般的なプログラミング)が初めてで、必要な情報を取得するためにPythonとBashスクリプトを使用しています。私はLinux用のWindowsサブシステム(WSL)を使用して実行しており、何らかの理由でスクリプトがgit-bashを使用して実行されています。
WebページのHtmlをダウンロードし、別のWebページへのリンクを含む2つのtxtファイルを返すPythonスクリプトに送信するbashスクリプトを作成しようとしています。その後、ソーススクリプトはtxtファイルのリンクの1つを繰り返し、各Webページのhtmlコンテンツをリンクの特定の部分名を付けたファイルとしてダウンロードします。しかし、最後のループは機能しません。
カールコマンドへのリンクを手動で作成すると機能します。ただし、スクリプトを実行しようとすると機能しません。
Bashスクリプトは次のとおりです。

#!/bin/bash

curl http://mythicspoiler.com/sets.html |
cat >>mainpage.txt
python creatingAListOfAllExpansions.py #returns two txt files containing the expansion links and the commander decks' links
rm mainpage.txt

#get the pages from the links
cat commanderDeckLinks.txt |
while read a ; do
    curl $a |          ##THIS DOESN'T WORK
    cat >>$(echo $a | cut --delimiter="/" -f4).txt
done

私はいくつかの異なるアプローチを試してみて、同様の問題を見ましたが、生涯にわたってこの問題を解決することはできません。常に同じエラーが表示されます。

curl: (3) URL using bad/illegal format or missing URL

CommanderDeckLinks.txtの内容は次のとおりです。

http://mythicspoiler.com/cmd/index.html
http://mythicspoiler.com/c13/index.html
http://mythicspoiler.com/c14/index.html
http://mythicspoiler.com/c15/index.html
http://mythicspoiler.com/c16/index.html
http://mythicspoiler.com/c17/index.html
http://mythicspoiler.com/c18/index.html
http://mythicspoiler.com/c19/index.html
http://mythicspoiler.com/c20/index.html

これはPythonスクリプトです。

#reads the main page of the website
with open("mainpage.txt") as datafile:
    data = datafile.read()

#gets the content after the first appearance of the introduced string
def getContent(data, x):
    j=0
    content=[]
    for i in range(len(data)):
        if(data[i].strip().startswith(x) and j == 0):
            j=i
        if(i>j and j != 0):
            content.append(data[i])
    return content

#gets the content of the website that is inside the body tag
mainNav = getContent(data.splitlines(), "<!--MAIN NAVIGATION-->")

#gets the content of the website that is inside of the outside center tags
content = getContent(mainNav, "<!--CONTENT-->")

#removes extra content from list
def restrictNoise(data, string):
    content=[]
    for i in data:
        if(i.startswith(string)):
            break
        content.append(i)
    return content

#return only lines which are links
def onlyLinks(data):
    content=[]
    for i in data:
        if(i.startswith("<a")):
            content.append(i)
    return content


#creates a list of the ending of the links to later fetch
def links(data):
    link=[]
    for i in data:
        link.append(i.split('"')[1])
    return link

#adds the rest of the link
def completLinks(data):
    completeLinks=[]
    for i in data:
        completeLinks.append("http://mythicspoiler.com/"+i)
    return completeLinks

#getting the commander decks
commanderDecksAndNoise = getContent(content,"<!---->")
commanderDeck = restrictNoise(commanderDecksAndNoise, "<!---->")
commanderDeckLinks = onlyLinks(commanderDeck)
commanderDecksCleanedLinks = links(commanderDeckLinks)

#creates a txt file and writes in it
def writeInTxt(nameOfFile, restrictions, usedList):
    file = open(nameOfFile,restrictions)
    for i in usedList:
        file.write(i+"\n")
    file.close()

#creating the commander deck text file
writeInTxt("commanderDeckLinks.txt", "w+", completLinks(commanderDecksCleanedLinks))

#getting the expansions
expansionsWithNoise = getContent(commanderDecksAndNoise, "<!---->")
expansionsWithoutNoise = restrictNoise(expansionsWithNoise, "</table>")
expansionsLinksWNoise = onlyLinks(expansionsWithoutNoise)
expansionsCleanedLinks = links(expansionsLinksWNoise)

#creating the expansions text file
writeInTxt("expansionLinks.txt", "w+", completLinks(expansionsCleanedLinks))

私の問題を解決するために追加情報が必要な場合はお知らせください。助けてくれた皆さんに感謝します

ベストアンサー1

ここでの問題は、bash(Linux)とウィンドウの行の終わりがそれぞれLFとCRLFで異なることです(これはすべて新しいものなのでよくわかりません)。したがって、行で区切られたエントリを含むPythonファイルを作成すると、生成されたファイルにCRLFの終わりがあり、bashスクリプトはLFのみを読み取るためURLを無駄にするため、bashスクリプトはファイルを読み取ることができません。ありません。 Bashコードを使用してこの問題を解決する方法がわかりませんが、私がしたことは、アンダースコア「_」で区切られた各項目を含むファイルを生成し(Pythonを使用して)、最後の項目nを追加して終わりを処理する必要がないようにするすることでした。ラインの。次に、最後の項目を除いて、下線で区切られた各項目を繰り返すbashでforループを実行しました。これで問題が解決しました。

おすすめ記事