Python 和 Jupyter 扩展的最新更新:2023 年 6 月版 Visual Studio Code | 您所在的位置:网站首页 › 盗版的win10能更新到最新吗 › Python 和 Jupyter 扩展的最新更新:2023 年 6 月版 Visual Studio Code |
下面是使用Python 和 Jupyter Notebook的demo: # 导入所需的库 import requests from bs4 import BeautifulSoup import pandas as pd import threading import time #亿牛云代理 爬虫代理加强版 # 定义爬虫代理IP的主机、端口、用户名和密码 proxy_host = "www.16yun.cn" proxy_port = "8080" proxy_user = "16YUN" proxy_pass = "16IP" # 定义代理IP的字典 proxies = { "http": f"http://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}", "https": f"https://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}" } # 定义一个空列表,用来存储采集到的数据 data_list = [] # 定义一个函数,用来采集指定网址的数据,并添加到列表中 def get_data(url): # 使用 requests 库发送 GET 请求,并使用代理 IP response = requests.get(url, proxies=proxies) # 使用 BeautifulSoup 库解析 HTML 文档 soup = BeautifulSoup(response.text, "html.parser") # 使用 find_all 方法找到所有的热点新闻的 div 元素 news_list = soup.find_all("div", class_="single-mode-rbox-inner") # 遍历每个 div 元素,提取标题、图片和时间,并添加到列表中 for news in news_list: title = news.find("div", class_="title-box").a.text # 提取标题 image = news.find("img", class_="lazy-load-img") # 提取图片元素 if image: # 如果有图片元素,提取图片的 src 属性值 image = image["src"] else: # 如果没有图片元素,设置图片为 None image = None time = news.find("div", class_="footer-bar-left").span.text # 提取时间 data_list.append([title, image, time]) # 添加到列表中 # 定义一个函数,用来导出数据到 excel 文件中 def export_data(): # 使用 pandas 库创建一个 DataFrame 对象,传入列表和列名 df = pd.DataFrame(data_list, columns=["标题", "图片", "时间"]) # 使用 to_excel 方法导出数据到 excel 文件中,指定文件名和索引列 df.to_excel("toutiao_top100.xlsx", index=False) # 定义一个函数,用来显示 Jupyter Notebook 的一些特性,比如进度条和魔法命令 def show_features(): # 使用 tqdm 库创建一个进度条对象,传入列表的长度作为总数 from tqdm.notebook import tqdm pbar = tqdm(total=len(data_list)) # 每隔一秒更新一次进度条,直到列表达到 100 个元素为止 while len(data_list) |
CopyRight 2018-2019 实验室设备网 版权所有 |