名称定义无效,而另一个名称的相同代码有效

时间:2019-07-18 06:58:24

标签: python json beautifulsoup

我正在编写一个Web抓取程序(python和bs4),以从网站获取数据,然后将其放入json。但是我在我不理解的for循环中遇到错误

最相关的部分是for循环,只有导入和bs4的设置未在此处显示

如您所见,两个for循环有点相同,但不知何故我收到一条错误消息,告诉我未定义名称行

这是完整的代码

import requests
import json
from bs4 import BeautifulSoup

#URL declaration
url01 = 'https://www.statistik.at/web_de/statistiken/wirtschaft/preise/baukostenindex/030979.html'
#url02 = 'https://www.statistik.at/web_de/statistiken/wirtschaft/preise/verbraucherpreisindex_vpi_hvpi/zeitreihen_und_verkettungen/022808.html'
#url03 = 'https://www.statistik.at/web_de/statistiken/wirtschaft/produktion_und_bauwesen/konjunkturdaten/baupreisindex/022804.html'

#BeautifulSoup4
response = requests.get(url01, timeout=5)
content = BeautifulSoup(response.content, 'html.parser')

#Find all td in class body in div table table-hover
for data in content.find_all('tbody', attrs={'class':'body'}):
    data.find('td', attrs={'class':'body'}) 
    data.find('b', attrs={'class':'body'})

    #print (data)

for row in content.find_all('tbody', attrs={'class':'body'}):
    row.find('td', attrs={'class':'body'})

    print (row)

#Phyton dictionary
dataDict = {}
#dataDict['data'] = data.text 
#dataDict['row'] = row.text

#save dictionary in json file
with open('indexData.txt', 'w') as f:
    json.dump(dataDict, f)

这是我的错误报告

Traceback (most recent call last):
  File "D:\SWP_Dateien\Praktikum Illwerke\WebScraper\WebScraper\scraper.py", line 28, in <module>
    dataDict['row'] = row.text
NameError: name 'row' is not defined
Press any key to continue . . .

0 个答案:

没有答案