【问题标题】:Can't write more than one line to CSVCSV 不能写入多于一行
【发布时间】:2015-04-14 23:13:20
【问题描述】:

我已经构建了一个网络抓取工具,可以提取网站上的所有图像。我的代码应该将每个 img URL 打印到标准输出并写入一个包含所有这些的 csv 文件,但现在它只是将找到的最后一个图像写入文件并将结果的数量写入 csv。

这是我目前使用的代码:

# This program prints a list of all images contained in a web page 
#imports library for url/html recognition
from urllib.request import urlopen
from HW_6_CSV import writeListToCSVFile
#imports library for regular expressions
import re
#imports for later csv writing
import csv
#gets user input
address = input("Input a url for a page to get your list of image urls       ex. https://www.python.org/:  ")
#opens Web Page for processing
webPage = urlopen(address)
#defines encoding
encoding = "utf-8"
#defines resultList variable
resultList=[]
#sets i for later printing
i=0
#defines logic flow
for line in webPage :
   line = str(line, encoding)
   #defines imgTag
   imgTag = '<img '
   #goes to next piece of logical flow
   if imgTag in line :
      i = i+1
      srcAttribute = 'src="'
      if srcAttribute in line:
      #parses the html retrieved from user input 
       m = re.search('src="(.+?)"', line)
       if m:
          reline = m.group(1)
          #prints results
          print("[ ",[i], reline , " ]")

data = [[i, reline]]

output_file = open('examp_output.csv', 'w')
datawriter = csv.writer(output_file)
datawriter.writerows(data)
output_file.close()
webPage.close()

如何让这个程序将找到的所有图像写入 CSV 文件?

【问题讨论】:

  • 对象data,在倒数第7行声明,只包含一行。
  • @bernie 那么如何让它识别 reline 中的所有数据?
  • @Celeo 抱歉,我正在尝试打印所有数据

标签: python csv web-scraping


【解决方案1】:

您只会看到 csv 中的最后一个结果,因为 data 永远不会在 for 循环的范围内正确更新:当您退出循环时,您只会写入一次。要将所有相关的 HTML 片段添加到您的列表 data,您应该缩进该行并使用列表的 appendextend 方法。

因此,如果您将循环重写为:

img_nbr = 0  # try to avoid using `i` as the name of an index. It'll save you so much time if you ever find you need to replace this identifier with another one if you chose a better name
data = []
imgTag = '<img ' # no need to redefine this variable each time in the loop
srcAttribute = 'src="' # same comment applies here

for line in webPage:
   line = str(line, encoding)
   if imgTag in line :
      img_nbr += 1  # += saves you typing a few keystrokes and a possible future find-replace.
      #if srcAttribute in line:  # this check and the next do nearly the same: get rid of one
      m = re.search('src="(.+?)"', line)
      if m:
          reline = m.group(1)
          print("[{}: {}]".format(img_nbr, reline)) # `format` is the suggested way to build strings. It's been around since Python 2.6.
          data.append((img_nbr, reline)) # This is what you really missed.

你会得到更好的结果。我添加了一些 cmets 来为您的编码技巧提供一些建议,并删除了您的 cmets 以使新的 cmets 脱颖而出。

但是,您的代码仍然存在一些问题:除非源代码结构非常好(即使那样......),否则不应使用正则表达式解析 HTML。现在,因为您要求用户输入,所以他们可以提供任何 url,而网页往往结构不佳。如果您想构建更强大的网络抓取工具,我建议您查看BeautifulSoup

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多