Python基础
-
保存内容到word
import mistunefrom docx import Documentfrom docx.shared import Inchesfrom bs4 import BeautifulSoupimport requestsfrom io import BytesIO# 使用 Mistune 解析器,确保支持 GitHub 风格的表格markdown_parser = mistune.create_markdown(renderer='html', plugins=['table'])def download_image(url): """根据 URL 下载图片并返回字节流""" respo
-
保存数据到excel表中
import pandas as pdfrom openpyxl import Workbookfrom openpyxl.styles import PatternFill, Alignment, Fontfrom openpyxl.utils.dataframe import dataframe_to_rowsfrom openpyxl.utils import get_column_letterimport requestsdef save_2d_array_to_excel(data_array, headers, file_path, sheet_name='Sheet1', rem
-
简单爬虫例子2
import requestsfrom bs4 import BeautifulSoupdef fetch_and_parse(url): # 发送HTTP请求到指定URL response = requests.get(url) # 检查请求是否成功 if response.status_code == 200: # 使用BeautifulSoup解析HTML内容 soup = BeautifulSoup(response.text, 'html.parser') # 查找所有的class为"layout_li ajaxpost"的li元素 url_cards = soup.find_all
-
简单爬虫例子1
import requestsfrom bs4 import BeautifulSoupdef fetch_and_parse(url): # 发送HTTP请求到指定URL response = requests.get(url) # 检查请求是否成功 if response.status_code == 200: # 使用BeautifulSoup解析HTML内容 soup = BeautifulSoup(response.text, 'html.parser') # 查找所有的class为"url-card"的div元素 url_cards = soup.find_all('div', {
-
生成带文字的纯色图片
from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilterdef create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path): """ Create a new image of specified size and color with centered text that has a border and shadow. :param size: A tuple con
-
读取excel表格内容
import pandas as pddef get_excel_data(file_path): try: # 读取 Excel 文件 df = pd.read_excel(file_path) # 获取表头 headers = df.columns.tolist() # 获取内容并转换为列表的字典,每一行对应一个字典 content = df.to_dict(orient='records') return {'headers': headers, 'content': content} except Exception as e: print(f"读取文件时发生错误: {e}") ret