2014-02-28 92 views
0

我想通过不同的URL(我将它们放在名为“cod”的列表中)写入CSV文件,并逐行写入到网络中(用于导出到Excel中)。如何遍历URL以写入CSV

我已经尝试过只有一个链接,但如果我想要与列表中的所有元素一起做,我需要迭代,并且有困难。

我的代码:

import urllib 
from bs4 import BeautifulSoup 
import csv 
urlfixed = "http://www.fatm.com.es/Datos_Equipo.asp?" 


cod = ["01GR0001","01GR0004","03GR0006","02GR0003","01GR0030","01GR0018","04GR0007","03GR0032","01AL0001","02AL0003"] 
loong = len(cod) 
i = 0 

sock = urllib.urlopen(urlfixed + "Cod=" + cod[i]) 
htmlSource = sock.read() 
sock.close() 
soup = BeautifulSoup(htmlSource) 
form = soup.find("form", {'id': "FORM1"}) 

valores = [item.get('value') for item in form.find_all('input')] 
valores.remove('Imprimir') 
valores.remove('Cerrar') 
values = valores 

out = open('tomate.csv', 'w') 
w = csv.writer(out) 
w.writerow([s.encode("utf-8") for s in values]) 
out.close() 

所以,一个从一个 “鳕鱼” 的信息排,这应该使10号线在 “tomate.csv”。

回答

3

只需使用一个for循环与迭代通过列表cod迭代,你是写在打开文件时它应该是追加:

urlfixed = "http://www.fatm.com.es/Datos_Equipo.asp?" 
cod = ["01GR0001","01GR0004","03GR0006","02GR0003","01GR0030","01GR0018","04GR0007","03GR0032","01AL0001","02AL0003"] 
for i in cod: 
    sock = urllib.urlopen(urlfixed + "Cod=" + i) 
    htmlSource = sock.read() 
    sock.close() 
    soup = BeautifulSoup(htmlSource) 
    form = soup.find("form", {'id': "FORM1"}) 

    valores = [item.get('value') for item in form.find_all('input')] 
    valores.remove('Imprimir') 
    valores.remove('Cerrar') 
    values = valores 

    out = open('tomate.csv', 'ab') 
    w = csv.writer(out) 
    w.writerow([s.encode("utf-8") for s in values]) 
    out.close() 
#the loop ends here 
+0

类型错误:列表索引必须是整数,而不是STR ..这是什么意思? – juasmilla

+0

对不起,但问题是我需要将所有信息从一个“cod”转换为CSV中的新行。有了这个,我只是有一条独特的路线。 – juasmilla

+1

又一次改变它应该是out = open('tomate.csv','ab')'而不是'out = open('tomate.csv','w')'。我试过它的工作 – Stormvirux