Python Web 爬虫秘籍
warning:
这篇文章距离上次修改已过190天,其中的内容可能已经有所变动。
import requests
from bs4 import BeautifulSoup
# 定义一个简单的函数来获取网页内容
def get_html(url):
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return None
except requests.RequestException:
return None
# 定义一个函数来解析网页并提取所需信息
def parse_html(html):
soup = BeautifulSoup(html, 'html.parser')
# 假设我们要提取的信息是标题
title = soup.find('title')
return title.text if title else None
# 使用示例
url = 'https://www.example.com'
html = get_html(url)
if html:
parsed_title = parse_html(html)
print(f"The title of the webpage is: {parsed_title}")
else:
print("Failed to retrieve the webpage content.")
这段代码展示了如何使用requests库获取网页内容,并使用BeautifulSoup进行解析,提取特定的网页信息。代码简洁,注重实用性,可以作为编写Web爬虫的基础教程。
评论已关闭