脚本编写面试题, 请编写一个 Python 脚本定时抓取某个网页的内容.
脚本编写面试题, 请编写一个 Python 脚本定时抓取某个网页的内容.
QA
Step 1
Q:: 面试题:
请编写一个 Python 脚本定时抓取某个网页的内容。
A:: 答案:
这是一个使用 Python 中的 requests 和 BeautifulSoup 库来抓取网页内容,并使用 schedule 库进行定时任务的示例脚本。
import requests
from bs4 import BeautifulSoup
import schedule
import time
url = 'http://example.com'
# 抓取网页内容
def fetch_content():
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
print(soup.prettify())
else:
print('Failed to retrieve the content')
# 定时任务,每小时抓取一次
schedule.every(1).hours.do(fetch_content)
while True:
schedule.run_pending()
time.sleep(1)
Step 2
Q:: 面试题:
如何保存抓取到的图片链接?
A:: 答案:
我们可以使用 BeautifulSoup 抓取网页中的所有图片链接并将其保存到一个文件中。
import requests
from bs4 import BeautifulSoup
url = 'http://example.com'
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
images = soup.find_all('img')
with open('image_links.txt', 'w') as f:
for img in images:
img_url = img.get('src')
if img_url:
f.write(img_url + '\n')
else:
print('Failed to retrieve the content')
Step 3
Q:: 面试题:
如何处理抓取网页内容中的动态数据?
A:: 答案:
对于动态网页,可以使用 Selenium 库模拟浏览器行为来抓取动态加载的内容。
from selenium import webdriver
from bs4 import BeautifulSoup
import time
url = 'http://example.com'
# 使用Selenium和Chrome驱动器
browser = webdriver.Chrome()
browser.get(url)
# 等待页面加载完成
time.sleep(5)
# 获取页面内容
html = browser.page_source
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())
browser.quit()
用途
面试这些内容的目的是评估候选人在实际项目中处理数据抓取任务的能力。这在实际生产环境中非常重要,尤其是当需要定期从网页获取最新数据时,比如在数据分析、市场调研、竞争对手分析等领域。\n相关问题
DevOps 运维面试题, 请编写一个 Python 脚本定时抓取某个网页的内容.
QA
Step 1
Q:: 请描述如何使用Python定时抓取某个网页的内容?
A:: 使用Python可以通过requests
库获取网页内容,通过BeautifulSoup
库解析网页内容,并使用schedule
库或time.sleep
来实现定时抓取。简单的实现方式如下:
import requests
from bs4 import BeautifulSoup
import time
import schedule
# 定义抓取函数
def fetch_web_content(url):
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
return soup.prettify() # 返回网页的内容
return None
# 定时任务
schedule.every(10).minutes.do(lambda: fetch_web_content('https://example.com'))
while True:
schedule.run_pending()
time.sleep(1)
上述脚本每10
分钟抓取一次指定网页的内容。
Step 2
Q:: 如何处理定时任务的异常情况,如网络连接失败?
A:: 为了处理定时任务中的异常情况,可以使用try-except
块来捕获异常,并添加日志记录或告警机制以便于在抓取失败时及时响应。例如:
import logging
logging.basicConfig(filename='fetch_errors.log', level=logging.ERROR)
try:
content = fetch_web_content('https://example.com')
except Exception as e:
logging.error(f'Error occurred: {e}')
Step 3
Q:: 如何保存抓取到的网页内容,并保留图片链接?
A:: 抓取网页内容后,可以将其保存到文件中,或者存储在数据库中。为了保留图片链接,可以使用BeautifulSoup
提取所有的img
标签,并保存其src
属性。例如:
soup = BeautifulSoup(response.text, 'html.parser')
images = [img['src'] for img in soup.find_all('img')]
with open('web_content.html', 'w') as file:
file.write(soup.prettify())
with open('image_links.txt', 'w') as img_file:
for img in images:
img_file.write(f'{img}\n')
Step 4
Q:: 如何高效地定时抓取多个网页的内容?
A:: 为了高效地抓取多个网页,可以使用多线程或异步IO。在Python中,可以使用concurrent.futures
中的ThreadPoolExecutor
或asyncio
库实现异步抓取。例如使用ThreadPoolExecutor
:
from concurrent.futures import ThreadPoolExecutor
urls = ['https://example1.com', 'https://example2.com']
with ThreadPoolExecutor(max_workers=5) as executor:
results = list(executor.map(fetch_web_content, urls))