From 4ef7b0a2e5c6af5dea3e3d8f9e6a9d6090c23aff Mon Sep 17 00:00:00 2001 From: 2509165015 <2509165015@student.edu.cn> Date: Thu, 19 Mar 2026 16:10:03 +0800 Subject: [PATCH] pachongwanchen --- 1243/爬虫.py.txt | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1243/爬虫.py.txt diff --git a/1243/爬虫.py.txt b/1243/爬虫.py.txt new file mode 100644 index 0000000..f529c50 --- /dev/null +++ b/1243/爬虫.py.txt @@ -0,0 +1,30 @@ +import requests +from bs4 import BeautifulSoup as bs +headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" +} + +url = 'https://www.baidu.com' +params = {'key':'value'} + +try: + response = requests.get(url, params=params, headers=headers, timeout=5) + response.raise_for_status() + print("״̬Âë:", response.status_code) + print('-'*20) + + html_content = response.text + soup = bs(html_content, 'lxml') + title = soup.find('title').string + print("Ò³Ãæ±êÌâ:", title) + print('='*20) + links = soup.find_all('a') + for link in links: + href = link.get('href') + if href: + print("Á´½Ó:", href) + print('='*20) +except requests.exceptions.RequestException as e: + print(f"ÇëÇó´íÎó: {e}") +except Exception as e: + print(f"½âÎö´íÎó: {e}") \ No newline at end of file