用python写爬虫

好的,下面是一个简单的Python爬虫示例,可以用来爬取网页数据:

import requests
from bs4 import BeautifulSoup

# 定义爬取函数
def scrape(url):
    # 发送HTTP请求
    response = requests.get(url)
    
    # 使用BeautifulSoup解析HTML内容
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 提取需要的数据
    data = soup.find('div', class_='data').text
    
    # 返回数据
    return data

# 设置要爬取的网页URL
url = 'http://example.com'

# 调用爬取函数
result = scrape(url)

# 打印结果
print(result)

这个例子使用了Requests库发送HTTP请求,并使用BeautifulSoup库解析HTML内容。你可以根据需要修改选择器以及提取的数据部分来适应不同的网页。

你可能感兴趣的:(Python,python,爬虫,开发语言)