Python-爬取天气和星座(自学)

本来想做一个微信机器人自动发消息的机器人,但发现注册的小i跟图灵机器人都开始收费了,wxpy库自带的机器人在扫码登陆的时候,无法登录,于是就只写了前面的这一些部分。跟之前的写的爬虫不一样的是这次没有使用正则表达式。

import requests
import itchat

def get_sentence():
    jinshanapi = 'http://web.juhe.cn:8080/constellation/getAll?consName=%E5%B7%A8%E8%9F%B9%E5%BA%A7&type=today&key=528801f423ea71a958aecdddf7e0b482'
    sentence = requests.get(jinshanapi)
    return sentence.json()


def get_weather_forecast():
    apikey = 'ffa2e199264d8575ebb6a165a272852c'
    url = 'https://www.tianqiapi.com/api?version=v6&appid=21375891&appsecret=fTYv7v5E&city=安阳'
    weather_forecast = requests.get(url)
    return weather_forecast.json()


if __name__ == '__main__':

    sentence = get_sentence()
    
    datetime = sentence['datetime']
    name = sentence['name']
    QFriend = sentence['QFriend']
    color = sentence['color']
    health = sentence['health']
    love = sentence['love']
    work = sentence['work']
    money = sentence['money']
    number = sentence['number']
    summary = sentence['summary']
    all = sentence['all']
    print('-----------今日星座-----------')
    print(' 今日:%s\n 星座:%s\t 上升星座:%s\n 颜色:%s\t 健康:%s\n 爱情:%s\t 事业:%s\n 金钱:%s\t 幸运数字:%s\n 今日总结:%s\n 总值:%s\n'%(datetime, name, QFriend, color , health, love, work ,money,number, summary, all))


    weatherforecast = get_weather_forecast()
    print('-----------今日天气-----------')
    # print(weatherforecast)
    city = weatherforecast['city']
    week = weatherforecast['week']
    wea = weatherforecast['wea']
    tem = weatherforecast['tem']
    tem1 = weatherforecast['tem1']
    tem2 = weatherforecast['tem2']
    win = weatherforecast['win']
    win_meter = weatherforecast['win_meter']
    humidity = weatherforecast['humidity']
    visibility = weatherforecast['visibility']
    air_pm25 = weatherforecast['air_pm25']
    air_level = weatherforecast['air_level']
    air_tips = weatherforecast['air_tips']

    print(' 您当前所在地:中国·%s\n 日期:%s\t 天气:%s\t 温度:%s\n 最高温度:%s\t 最低温度:%s\n 风:%s\t \t风速:%s\t 湿度:%s\n 能见度:%s\t 空气指数:%s\t 污染程度:%s\n 建议:%s'%
          (city, week, wea, tem , tem1, tem2, win ,win_meter,humidity,visibility,air_pm25,air_level,air_tips))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
需要使用Python编程语言来爬取知乎问题下的所有回答。具体步骤如下: 1. 首先需要安装Python的requests和beautifulsoup4库,用于发送HTTP请求和解析HTML页面。 2. 获取知乎问题页面的URL,可以手动复制粘贴,或者使用爬虫自动获取。 3. 使用requests库发送GET请求,获取知乎问题页面的HTML源代码。 4. 使用beautifulsoup4库解析HTML源代码,获取所有回答的信息。 5. 对每个回答进行解析,获取回答的文本、作者、点赞数、评论数等信息。 6. 将获取到的信息存储到本地文件或数据库中。 下面是一段示例代码,可以爬取知乎某个问题下的所有回答: ```python import requests from bs4 import BeautifulSoup # 知乎问题页面的URL url = 'https://www.zhihu.com/question/xxxxxx' # 发送GET请求,获取页面HTML源代码 response = requests.get(url) html = response.text # 解析HTML页面,获取所有回答的信息 soup = BeautifulSoup(html, 'html.parser') answers = soup.find_all('div', class_='List-item') # 遍历每个回答,解析并存储信息 for answer in answers: # 解析回答文本、作者、点赞数、评论数等信息 text = answer.find('div', class_='RichContent-inner').get_text() author = answer.find('div', class_='ContentItem-head').get_text() upvotes = answer.find('button', class_='Button VoteButton VoteButton--up').get_text() comments = answer.find('button', class_='Button ContentItem-action Button--plain Button--withIcon Button--hoverCard').get_text() # 将信息存储到本地文件或数据库中 with open('answers.txt', 'a', encoding='utf-8') as f: f.write(f'{author}\n{text}\n赞同数:{upvotes} 评论数:{comments}\n\n') ``` 需要注意的是,爬取知乎数据属于个人行为,需要遵守知乎的相关规定,不得用于商业用途。另外,爬取速度也需要适当控制,避免给服务器造成过大的负担。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值