使用For循环创建编号的词典[python]

时间:2015-11-08 23:28:40

标签: python dictionary

对于18个不同的链接,我试图创建18个不同的词典(每个词典在链接中划分的每个区域),每个词典包含统计数据的密钥和值,如人口,住房单位面积等。 key是统计信息的名称,值是值。我已经编写了遍历所有18个链接的部分并检索了人口和住房单元(及其值),但我无法弄清楚如何制作字典。我尝试使用for循环:

def bgScrape(self):
        i = 0
        for l in self.hrefs:
            html = urlopen('http://www.usa.com' + l)
            soup = BeautifulSoup(html.read(), 'lxml')
            stat = soup.find('table')
            stat = stat.find_next('table')
            population = stat.find('td')
            population = population.find_next('td')
            population1 = population.find_next('a')
            population1 = population1.find_next('a')
            houseunits = population1.find_next('td')
            houseunits1 = population1.find_next('a')
            print(houseunits1)
            for i in range(18):
                stats[i] = {}
                stats[i]['Population'] = population1.text

但这给了我NameError: name 'stats' is not defined,这让我相信你不能像这样在For循环中制作字典。是这种情况,还是我只是写得不正确?或者是否有更好的方法来完全实现这一目标?

1 个答案:

答案 0 :(得分:2)

您可以替换整个第二个循环

private class MyTask extends AsyncTask<Void, Void, Value> 
{
    boolean success = false;

    @Override
    protected String doInBackground(String toSubmit) 
    {
        StringBuilder respData = new StringBuilder();
        URL url = new URL("MY_URL");
        URLConnection conn = url.openConnection();
        HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;

        httpUrlConnection.setUseCaches(false);
        httpUrlConnection.setRequestProperty("User-Agent", "YourApp"); 
        httpUrlConnection.setConnectTimeout(30000);
        httpUrlConnection.setReadTimeout(30000);

        httpUrlConnection.setRequestMethod("POST");
        httpUrlConnection.setDoOutput(true);

        OutputStream os = httpUrlConnection.getOutputStream();
        InputStream postStream = toInputStream(toSubmit, "UTF-8");
        try {
            copy(postStream, os);
        } finally {
            postStream.close();
            os.flush();
            os.close();
        }

        httpUrlConnection.connect();

        int responseCode = httpUrlConnection.getResponseCode();

        if (200 == responseCode) {
            InputStream is = httpUrlConnection.getInputStream();
            InputStreamReader isr = null;
            try {
                isr = new InputStreamReader(is);
                char[] buffer = new char[1024];
                int len;
                while ((len = isr.read(buffer)) != -1) {
                    respData.append(buffer, 0, len);
                }
            } finally {
                if (isr != null)
                {
                    isr.close();
                    success = true;
                }
            }
            is.close();
        }
        else {
            // use below to get error stream
            // inputStream = httpUrlConnection.getErrorStream();
        }
        return respData.toString();
    }

    @Override
    protected void onPostExecute(String result) 
    {
        Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);
        processValue(result);
    }
}

private void processValue(String theResult) 
{
   //handle value
}

String toSubmit = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    submit.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //get input from editText boxes to send to php file on server
            toSubmit = name.getText().toString() + " " + email.getText().toString() + " " + idea.getText().toString();

            try{
                new MyTask().execute();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    });
}

by:

for i in range(18):
    stats[i] = {}
    stats[i]['Population'] = population1.text