JIJIGUN 2019-07-11 15:02 采纳率: 0%
浏览 453

python能成功爬取天气 不知道怎么加循环

现在能爬去数据, 但需要手动一个一个换城市和时间, 希望能加个循环,一次性爬玩选定城市所有时间的天气。
网站来自 天气网->历史天气。 网站特点:http://lishi.tianqi.com/'+position+'/'+date+'.html

我不知道怎么加循环

代码现在如下:

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import csv
import os

from openpyxl import workbook  # 写入Excel表所用

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_setting.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.set_page_load_timeout(60)
driver.set_script_timeout(60)




def getId():
    position = 'wulumuqi' #这里填写地方的缩拼音例如:安泽
    date = '201901' #这里填写日期例如:2011年的2月
    query_url = 'http://lishi.tianqi.com/'+position+'/'+date+'.html'
    wb = os.path.join('C:\\Users\\w4376\\OneDrive\\Desktop\\weather collecting\\') #指定路径打开
    wb = workbook.Workbook()  # 创建Excel对象
    ws = wb.active  # 获取当前正在操作的表对象
    ws.append(['日期', '最高气温', '最低气温', '天气', '风向', '风力'])
    try:
        driver.get('http://tianqi.com/')
        time.sleep(2)
        driver.get(query_url)
        js = "window.scrollTo(0, 1800000);"
        driver.execute_script(js)
        time.sleep(2)
        res = driver.page_source
        soup = BeautifulSoup(res, 'html.parser')
        box = soup.find('div',class_='tqtongji2').find_all('ul')[1:]
        for item in box:
            date_detail = item.find_all('li')[0].get_text() #日期
            print(date_detail)
            max_temperature =item.find_all('li')[1].get_text() #最高气温
            print(max_temperature)
            min_temperature = item.find_all('li')[2].get_text()  # 最低气温
            print(min_temperature)
            temperature = item.find_all('li')[3].get_text()  # 天气
            print(temperature)
            wind_direction =  item.find_all('li')[4].get_text() #风向
            print( wind_direction)
            wind_power = item.find_all('li')[5].get_text()  # 风力
            if wind_power == ('微风'):
                wf=('0级')
                wind_power=wf
                print(wf)
            else:
                print(wind_power)
            if wind_direction == ('无持续风向'):
                wd=('无')
                wind_direction=wd
                print(wd)
            ws.append([date_detail, max_temperature,min_temperature, temperature, wind_direction,wind_power])
            # spamwriter = csv.writer(csvfile, dialect='excel')
            # spamwriter.writerow([date_detail, max_temperature,min_temperature, temperature, wind_direction,wind_power]) 

        wb.save(position+date+'.xlsx')  # 存入所有信息后,保存为filename.xlsx
    except Exception as e:
        print(e)
getId()


  • 写回答

1条回答 默认 最新

  • BuXianShan 2019-07-11 15:23
    关注

    用一个列表保存城市名字,例如city = ["beijing","shanghai","nanjing"……]。然后用个for循环就行了。
    getID函数加个参数(position)

    for position in city:
         getID(position)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作