VSCode中安装相关的插件就可以配合chatgpt来实现一些简单的爬虫操作,只能应对比较简单的场景.
安装插件
chatGPT-ChatMoss
给AI发送
python 爬取无锡七日天气
回复如下:
要爬取无锡七日天气,可以使用Python中的第三方库 requests 和 Beautiful Soup。以下是详细步骤:
import requests
from bs4 import BeautifulSoup
url = "http://www.weather.com.cn/weather/101190201.shtml"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
weather_data = soup.select(".t>li")
for weather in weather_data:
# 获取日期、天气状况、最高温度和最低温度
date = weather.select_one(".date").get_text()
condition = weather.select_one(".wea").get_text()
high_temperature = weather.select_one(".tem span").get_text()
low_temperature = weather.select_one(".tem i").get_text()
# 打印天气信息
print(date, condition, high_temperature, low_temperature)
完整代码如下:
import requests
from bs4 import BeautifulSoup
url = "http://www.weather.com.cn/weather/101190201.shtml"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
weather_data = soup.select(".t>li")
for weather in weather_data:
date = weather.select_one(".date").get_text()
condition = weather.select_one(".wea").get_text()
high_temperature = weather.select_one(".tem span").get_text()
low_temperature = weather.select_one(".tem i").get_text()
print(date, condition, high_temperature, low_temperature)
但是上述实际运行是存在一些小问题的。
打印结果如下: 这个很明显就是date这个数据为None
None class="wea" title="多云">多云</p> 28</span> 17℃</i>
None class
="wea" title="多云">多云</p> 27</span> 15℃</i>
None class
="wea" title="多云">多云</p> 24</span> 12℃</i>
None class
="wea" title="阴转小雨">阴转小雨</p> 20</span> 15℃</i>
None class
="wea" title="阴">阴</p> 15</span> 13℃</i>
None class
="wea" title="小雨">小雨</p> 19</span> 14℃</i>
None class
="wea" title="小雨转晴">小雨转晴</p> 16</span> 8℃</i>
继续追问机器人:
优化下代码,没有获取到天数,并且这个get_text有报错.
机器人直接回复:
以下是优化后的代码,已解决上述问题:
import requests
from bs4 import BeautifulSoup
url = "http://www.weather.com.cn/weather/101190201.shtml"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
weather_data = soup.select(".t li")
for weather in weather_data:
# 获取日期、天气状况、最高温度和最低温度
date = weather.select_one("h1").get_text()
condition = weather.select_one(".wea").get_text()
high_temperature = weather.select(".tem span")[0].get_text()
low_temperature = weather.select(".tem i")[0].get_text()
# 打印天气信息
print(date, condition, high_temperature, low_temperature)
在优化代码时,我们对原有代码进行了以下改进:
这一次就非常好用了.
打印出的结果如下:
19日(今天) 多云 28 17℃
20日(明天) 多云 27 15℃
21日(后天) 多云 24 12℃
22日(周六) 阴转小雨 20 15℃
23日(周日) 阴 15 13℃
24日(周一) 小雨 19 14℃
25日(周二) 小雨转晴 16 8℃