Python爬虫抓取气象

阅读: 评论:0

Python爬虫抓取气象

Python爬虫抓取气象

文章目录

  • 前言
  • 说啥呢?直接扔代码吧
    • 看不懂的话你细品 留言也可以 进群也可以
  • 总结


前言

麻雀虽小 五脏俱全 这篇爬虫文章涉及的技术不少

bs4抓取数据 (之前一直用xpath感觉一种东西吃多了会腻)
定时器(一次执行终身执行 懒人必备)
mysql(数据库 存数据的地方)
对象(面向对象编程)


说啥呢?直接扔代码吧

看不懂的话你细品 留言也可以 进群也可以

# -*- coding: utf-8 -*-
"""
# @Time : 2021/4/6 10:10 # @Author : 一蓑烟雨任平生# @File : 天气.py # @Software: PyCharm
"""
import datetime
import threading
from datetime import dateimport pymysql
import requests
from bs4 import BeautifulSoupconn = t(host='127.0.0.1', user='root', passwd='123456', db='feifei', charset='utf8')
cur = conn.cursor()
print("数据库已连接")class ZhangZhang:def __init__(self):self.head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0'}self.babaUrl = ''def getTime(self):# 获取现在时间now_time = w()# 获取明天时间next_time = now_time + datetime.timedelta(days=+1)next_year = next_time.date().yearnext_month = next_time.date().monthnext_day = next_time.date().day# 获取明天3点时间next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + " 03:00:00","%Y-%m-%d %H:%M:%S")timer_start_time = (next_time - now_time).total_seconds()print(timer_start_time)return timer_start_time;def get_all_url(self):ee = []page_one = self.bs_Url('.htm')bb = [page_one.find('td', class_='bk_7').find_all("table")[i] for i in range(1, 8, 2)]time4 = day() - datetime.timedelta(days=4)for i in bb:cc = i.find("td", class_='hui_14').text[1:-1]if str(time4) == cc:dd = i.find('a', class_='link03')['href'][1:]ee.append(dd)ff = [self.babaUrl + fp for fp in ee]return ffdef get_info(self, list):for i in list:pic = i.split('/t')[0] + '/'shuju = self.bs_Url(i)title = shuju.find('td', class_='hui_15_cu').textpublish_time = shuju.find('td', class_='hui_12-12').text.split(":")[1][:10]source = shuju.find('td', class_='hui_12-12').text.split(":")[3]content = str(shuju.find('div', class_='TRS_Editor'))content = place("./", pic)sql = "insert into weather_info(title,publish_time,source,content) VALUES (%s,%s,%s,%s)&#ute(sql, (title, publish_time, source, content))connmit()cur.close()conn.close()def bs_Url(self, wangzhi):yy = (wangzhi, self.head).contentpage_info = BeautifulSoup(yy, "html.parser")return page_infodef run(self):conn.ping(reconnect=True)print("------爬虫程序开始------")# 获取今日Url然后详情__all_url());# 获取信息timer_start_time = Time();timer = threading.Timer(timer_start_time, self.run)timer.start()if __name__ == '__main__':ZhangZhang().run()

总结

问:楼主,你写代码命名怎么aa BB cc dd啊,好不规范啊

答:我的代码风格就是ABCDEFG,英语不好没时间搜百度,没给你搞成ZXCVBNM就已经很好了吧?

QQ群,对于刚学爬虫的小白可以来群里交流,我也是小白,只是在通往大佬的路上一直跌到,导致现在的鼻青脸肿,成了大胖子


本文发布于:2024-02-02 00:10:49,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170680960840084.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:爬虫   气象   Python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23