2017-04-22 12 views
-1

次のコードは正常に動作します。特定の日付(つまり、01/04/2017)に新聞を廃棄することですが、daterangeを作成して毎日ループして空になります値は、コードの下に、そして私はおそらく簡単ですが、私は、勉強はまだ、これらの問題が表示されない、'for loop'空の値を返すループ

url = 'http://servicios.lanacion.com.ar/archivo-f01/04/2017-c30' 
sauce = uReq(url) 
soup = bs.BeautifulSoup(sauce,'html.parser') 
date = soup.findAll('div',{'class':'titFecha'}) 
date = str(date) 
fecha = '"titFecha">' 
date = date[date.find(fecha)+len(fecha):date.find(fecha)+21] 
day = date[:2] 
month = date[3:5] 
year = date[6:12] 
print date 

filename = 'La Nacion_%s_%s_%s.csv' % (day, month, year) 
f = open(filename,'w') 
headers = "Date, Title, Encabezado\n" 
f.write(headers) 

acumulados = soup.findAll('li',{'class':'acumulados'}) 
for acum in acumulados: 
    title = acum.a['href'] 
    #  title = title.text 
    print title 
    encabezado = acum.p 
    #  encabezado = encabezado.text 
    print encabezado,'\n' 
    f.write(str(date) + ',' + str(title).replace(',',' ') + ',' + str(encabezado).replace(',',' ') + '\n') 

f.close() 

を入れて、ここではループであり、感謝!

date_range = pd.date_range('2017-04-01',periods=2, freq='d') 
date_range = date_range.strftime("%d/%m/%y") 
for i in date_range: 
    url = 'http://servicios.lanacion.com.ar/archivo-f%r-c30' % i 
    sauce = uReq(url) 
    soup = bs.BeautifulSoup(sauce,'html.parser') 
    date = soup.findAll('div',{'class':'titFecha'}) 
    date = str(date) 
    fecha = '"titFecha">' 
    date = date[date.find(fecha)+len(fecha):date.find(fecha)+21] 
    day = date[:2] 
    month = date[3:5] 
    year = date[6:12] 
    print date 

    filename = 'La Nacion_%s_%s_%s.csv' % (day, month, year) 
    f = open(filename,'w') 
    headers = "Date, Title, Encabezado\n" 
    f.write(headers) 

    acumulados = soup.findAll('li',{'class':'acumulados'}) 
    for acum in acumulados: 
     title = acum.a['href'] 
     print title 
     encabezado = acum.p 
     print encabezado,'\n' 
     f.write(str(date) + ',' + str(title).replace(',',' ') + ',' + str(encabezado).replace(',',' ') + '\n') 
f.close() 
+0

何「空の価値を得る」という意味ですか?どこ?出力ファイルには?あなたの 'print'ステートメントはどうですか? –

+0

問題を[MCVE]に絞り込みます。すべての質問でこの指示を繰り返す必要はありません! –

答えて

0

ループのない文字列をnumpy.string_さで私のフォーマットは、次の行url%s%rを変更し、以下のようにstrftime("%d/%m/%Y")strftime("%d/%m/%y")を変更することができます。

date_range = pd.date_range('2017-04-01',periods=2, freq='d') 
date_range = date_range.strftime("%d/%m/%Y") 
for i in date_range:  
    url = 'http://servicios.lanacion.com.ar/archivo-f%s-c30' % i 
+0

それはまさに間違いでした!フォーマット'strftime( "%d /%m /%y") 'はURLに必要な間違った文字列を返していたので、間違ったリンクで検索していたため、BSオブジェクトが空でした。私が'strftime( "%d /%m /%Y") 'に変更するとすぐに、私は期待される結果を得ました。ありがとう@ Tiny.D! @BoundaryImpositionのおかげで、質問の投稿とデバッグについて読むのが非常に役に立ちました。 –

関連する問題