<pre>以下代码也是参考自网友分享,已经在本地测试通过。无页面显示,如果想用的话欢迎。有问题欢迎与我联系。
讯享网
# -- coding: utf-8 -- import requests from bs4 import BeautifulSoup
def get_weather_info(url, choose_all=True):

讯享网try:
response = requests.get(url, timeout=2)
# 发起请求并设置超时时间为两秒
soup = BeautifulSoup(response.text, 'html.parser')
# 解析HTML
# 获取.day div的文本内容
day_divs = soup.find_all('div', class_='day')
day_text_list = [div.get_text() for div in day_divs]
# 输出今天的城市和天气信息
if choose_all:
result = [",".join([item.strip() for item in day_text.split("
”) if item.strip()]) for day_text in day_text_list]
#print(result) #print("result") return result else: for day_text in day_text_list: day_info = [item.strip() for item in day_text.split(" ”) if item.strip()]
讯享网 print("day_info") #print(day_info) return day_info except requests.exceptions.RequestException as e: print("Error occurred:", e) return None 城市名和链接可以在https://weather.cma.cn/中添加更多城市,名字和链接对应的
href_list = [‘58367’, ‘51463’, ‘52889’, ‘54511’, ‘58238’, ‘58606’, ‘58321’, ‘53463’, ‘50953’, ‘53772’, ‘59287’, ‘56294’,
'55591', '56778', '58457', '57494', '54342', '54823', '59758', '59493', '53698', '58847', '52866', '57036', '57816', '57083', '57516', '53614', '54161', '57679', '54517', '45005', '45011', '58968', '59431', '062721', '003772', '068816', '061641', 'G05021', '065563', '063260', '064950', 'G05020', '063980', '061832', '065472', '067665', '067341', '064500', 'G05019', '061442', 'G05025', '063125', '063450', '063740', '065125', '064700', 'G05024', '067775', '060390', 'G05018', '063894', '064390', '008589', '063705', 'G05017', '065387', '064387', '060135', '067083', '060715', '040103', '062010', '062366', '064810', 'G05009', '061291', '047108', '044292', '048698', '097390', '048647', '048074', '048991', '048820', '048940', '096315', '041571', '043466', '041923', '044454', '043555', '041217', '040582', '017130', '041170', '041256', '040100', '040438', '041150', '040754', '040650', '040948', '037864', '037545', '037788', '035188', '038353', '038836', '038457', '048455', '096741', '098425', '041404', 'G05010', '027612', '011035', '016716', '012375', '013274', '011520', '015614', '011816', '013615', '014240', '014654', '013462', '026038', '026730', '014014', '012843', '013586', '015420', '026422', '033345', '026850', '033815', 'G05005', '008535', 'G05011', '093439', '094035', 'G05007', 'G05022', 'G05004', 'G05014', 'G05013', 'G05003', 'G05012', '085574', 'G05023', '006240', '086580', '080415', 'G05016', 'G84071', '084628', '078762', '078806', 'G05015', 'G05006', '078970', '078862', 'G05026', 'G05008', 'G05002', '078224', 'G05001', '047662', '072405', '006180', '094926', '076680', '040080', '001492', '040270', '061052', '083377', '007149', '087582', '006451', '047058', '042299', '076405', '002464', '042182', '010385', '065344', '071628', '064650', '065503', '016242', '040183', '067001', '002974', '061990', '064210', '038880', '008221', 'map', '58367', '51463', '52889', '54511', '58238', '58606', '58321', '53463', '50953', '53772', '59287', '56294', '55591', '56778', '58457', '57494', '54342', '54823', '59758', '59493', '53698', '58847', '52866', '57036', '57816', '57083', '57516', '53614', '54161', '57679', '54517', '45005', '45011', '58968', '59431', '54511', '53463', '53772', '53698'</span></span></pre>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/144949.html