Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python csv.DictWriter不输入值是否有原因?_Python_Csv_Web Scraping - Fatal编程技术网

Python csv.DictWriter不输入值是否有原因?

Python csv.DictWriter不输入值是否有原因?,python,csv,web-scraping,Python,Csv,Web Scraping,我正在构建一个web scraper,它检索一个hashtag和与该hashtag关联的两个值。然后将信息放入csv文件中 我遇到的问题是,将为hashtag创建一个单元格,但标记本身不会被输入 with open('Test.csv', 'w', newline='')as csvfile: fieldnames = ['Tags', 'value1', 'value2'] info = csv.DictWriter(csvfile, fieldnames=fieldnames

我正在构建一个web scraper,它检索一个hashtag和与该hashtag关联的两个值。然后将信息放入csv文件中

我遇到的问题是,将为hashtag创建一个单元格,但标记本身不会被输入

with open('Test.csv', 'w', newline='')as csvfile:
    fieldnames = ['Tags', 'value1', 'value2']
    info = csv.DictWriter(csvfile, fieldnames=fieldnames)
    info.writeheader()

    for i in range(1, 3):

        tags = soup.find_all('span', class_='tag')[i].get_text()
        tags = tags.replace('#', '')
        time.sleep(2)

        value1 = soup.find_all('span', class_="value1")[i].get_text()
        time.sleep(2)

        value2 = soup.find_all('span', class_="value2")[i].get_text()

        info.writerow({'Tags': tags, 'value1': value1, 'value2': value2})
        #there is a problem with tags. They are not created in the csv file, it's just a blank space
        print(tags, ' ', value1, ' ', value2)
其他一切正常,甚至打印语句也会显示所有信息。

在这一行:

info.writerow({'Tags':Tags,'value1':value1,'value2':value2})
未引用两个变量
value1
value2
。也许你想要的是

info.writerow({'Tags':Tags,'value1':viewers,'value2':rooms})
这也解释了为什么print语句运行良好,因为在其中使用了
viewers
rooms
变量

但我觉得奇怪的是,在这一行中你没有得到任何
namererror
error…

info.writerow({'Tags':Tags,'value1':value1,'value2':value2})
未引用两个变量
value1
value2
。也许你想要的是

info.writerow({'Tags':Tags,'value1':viewers,'value2':rooms})
这也解释了为什么print语句运行良好,因为在其中使用了
viewers
rooms
变量


但我觉得奇怪的是,你没有收到任何
名称错误
错误…

谢谢,我没有收到名称错误,因为我编辑了要发布的代码。我只是没有很好地编辑代码。谢谢,我没有收到一个名称错误,因为我编辑了要发布的代码。我只是没有很好地编辑代码。