| import os |
| import requests |
| |
| folder_path = r'E:\test' |
| file_path = os.path.join(folder_path, 'guoli.txt') |
| |
| |
| if not os.path.exists(folder_path): |
| os.makedirs(folder_path) |
| |
| r = requests.get('https://www.baidu.com') |
| r.encoding = 'utf-8' |
| |
| if r.status_code == 200: |
| with open(file_path, 'a', encoding='utf-8') as file: |
| file.write("重新写入:\n") |
| file.write(r.text) |
| print("内容已成功保存到E:\\test\\guoli.txt文件中。") |
| else: |
| print(f"请求失败,状态码:{r.status_code}") |
上面的Python代码片段执行了以下功能:
- 导入必要的库:
- os:用于与操作系统交互,比如检查文件或文件夹是否存在,以及创建文件夹。
- requests:用于发送HTTP请求,这里用来获取网页内容。
- 设置文件夹和文件路径:
- folder_path:指定了一个文件夹路径(E:\test),这是你想要保存文件的位置。
- file_path:使用os.path.join函数将folder_path和文件名(guoli.txt)结合起来,形成完整的文件路径。
- 检查并创建文件夹:
- 使用os.path.exists(folder_path)检查folder_path指定的文件夹是否存在。
- 如果文件夹不存在,则使用os.makedirs(folder_path)创建它。注意,makedirs会创建所有必要的父文件夹。
- 发送HTTP GET请求:
- 处理HTTP响应:
- 检查响应的状态码是否为200(表示请求成功)。
- 如果状态码为200,则以追加模式('a')打开file_path指定的文件。如果文件不存在,将创建它。
- 向文件中写入一行文本("重新写入:\n"),然后写入HTTP响应的文本内容(r.text)。
- 打印一条消息到控制台,表示内容已成功保存到文件中。
- 处理请求失败的情况:
如果HTTP响应的状态码不是200,则打印一条消息到控制台,显示请求失败和相应的状态码。
获取B站的网页信息
User-Agent需要修改成自己电脑的信息
| import requests |
| |
| url = 'https://www.bilibili.com' |
| |
| headers = { |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' |
| } |
| |
| r = requests.get(url, headers=headers) |
| |
| if r.status_code == 200: |
| with open('bilibili_content.txt', 'w', encoding='utf-8') as file: |
| file.write(r.text) |
| print("内容已成功保存到bilibili_content.txt文件中。") |
| else: |
| print(f"请求失败,状态码:{r.status_code}") |
爬虫下载百度百科秒懂视频
| import os |
| import requests |
| |
| |
| video_url = 'https://baikevideo.cdn.bcebos.com/media/mda-XZHGU7ehu4iIlSTE/d9ee527a7e7a74bca43883c15ca6df8c.mp4' |
| |
| |
| folder_path = r'E:\test' |
| |
| |
| if not os.path.exists(folder_path): |
| os.makedirs(folder_path) |
| |
| |
| video_file_path = os.path.join(folder_path, 'downloaded_video.mp4') |
| |
| |
| try: |
| with requests.get(video_url, stream=True) as r: |
| |
| if r.status_code == 200: |
| |
| with open(video_file_path, 'wb') as f: |
| |
| for chunk in r.iter_content(chunk_size=1024*1024): |
| if chunk: |
| f.write(chunk) |
| print(f"视频已成功保存到{video_file_path}") |
| else: |
| print(f"请求失败,状态码:{r.status_code}") |
| except requests.RequestException as e: |
| |
| print(f"请求发生错误:{e}") |
爬取网页文字
| from selenium import webdriver |
| from selenium.webdriver.common.by import By |
| |
| |
| driver = webdriver.Chrome() |
| |
| |
| driver.get("https://blog.csdn.net/qq_61141142/article/details/134117295") |
| |
| |
| driver.implicitly_wait(10) |
| |
| |
| title_text = driver.title |
| |
| print(title_text) |
| |
| |
| paragraphs_text = [p.text for p in driver.find_elements(By.TAG_NAME, 'p')] |
| |
| print(paragraphs_text) |
| |
| |
| driver.quit() |
| from selenium import webdriver |
| from selenium.webdriver.common.by import By |
| |
| |
| driver = webdriver.Chrome() |
| |
| |
| driver.get("https://blog.csdn.net/qq_61141142/article/details/134117295") |
| |
| |
| driver.implicitly_wait(10) |
| |
| |
| title_text = driver.title |
| |
| print("页面标题:", title_text) |
| |
| |
| paragraphs_text = [p.text for p in driver.find_elements(By.TAG_NAME, 'p')] |
| |
| print("\n段落文本:") |
| for i, paragraph in enumerate(paragraphs_text, start=1): |
| print(f"段落{i}: {paragraph}") |
| |
| |
| driver.quit() |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)