@Robin_TY老师第九章案例8空气质量指数计算v7.0获取所有城市aqi只能获取前面十几个城市,并且很多报错(老师的源码也没法正常运行)

import requests
from bs4 import BeautifulSoup


def get_city_aqi(city_pinyin):
    """
        获取城市的AQI
    """
    url = 'http://pm25.in/' + city_pinyin
    r = requests.get(url, timeout=30)
    soup = BeautifulSoup(r.text, 'lxml')
    div_list = soup.find_all('div', {'class': 'span1'})

    city_aqi = []
    for i in range(8):
        div_content = div_list[i]
        caption = div_content.find('div', {'class': 'caption'}).text.strip()
        value = div_content.find('div', {'class': 'value'}).text.strip()

        city_aqi.append((caption, value))
    return city_aqi


def get_all_cities():
    """
        获取所有城市
    """
    url = 'http://pm25.in/'
    city_list = []
    r = requests.get(url, timeout=30)
    soup = BeautifulSoup(r.text, 'lxml')

    city_div = soup.find_all('div', {'class': 'bottom'})[1]
    city_link_list = city_div.find_all('a')
    for city_link in city_link_list:
        city_name = city_link.text
        city_pinyin = city_link['href'][1:]
        city_list.append((city_name, city_pinyin))
    return city_list


def main():
    """
        主函数
    """
    city_list = get_all_cities()
    for city in city_list:
        city_name = city[0]
        city_pinyin = city[1]
        city_aqi = get_city_aqi(city_pinyin)
        print(city_name, city_aqi)


if __name__ == '__main__':
    main()
 
 
Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\urllib3\response.py", line 360, in _error_catcher
    yield
  File "D:\Anaconda3\lib\site-packages\urllib3\response.py", line 669, in read_chunked
    chunk = self._handle_chunk(amt)
  File "D:\Anaconda3\lib\site-packages\urllib3\response.py", line 624, in _handle_chunk
    returned_chunk = self._fp._safe_read(self.chunk_left)
  File "D:\Anaconda3\lib\http\client.py", line 620, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
  File "D:\Anaconda3\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\requests\models.py", line 750, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "D:\Anaconda3\lib\site-packages\urllib3\response.py", line 490, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "D:\Anaconda3\lib\site-packages\urllib3\response.py", line 694, in read_chunked
    self._original_response.close()
  File "D:\Anaconda3\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "D:\Anaconda3\lib\site-packages\urllib3\response.py", line 365, in _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='pm25.in', port=80): Read timed out.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/DevProjects/python/python_course/lect08/aqi_v7.0.py", line 61, in <module>
    main()
  File "D:/DevProjects/python/python_course/lect08/aqi_v7.0.py", line 56, in main
    city_aqi = get_city_aqi(city_pinyin)
  File "D:/DevProjects/python/python_course/lect08/aqi_v7.0.py", line 16, in get_city_aqi
    r = requests.get(url, timeout=30)
  File "D:\Anaconda3\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "D:\Anaconda3\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\Anaconda3\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\Anaconda3\lib\site-packages\requests\sessions.py", line 686, in send
    r.content
  File "D:\Anaconda3\lib\site-packages\requests\models.py", line 828, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "D:\Anaconda3\lib\site-packages\requests\models.py", line 757, in generate
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='pm25.in', port=80): Read timed out.

Process finished with exit code 1

要回复问题请先登录注册