以下是一个简单的爬虫代码示例,使用 PyCharm 进行编写和运行:

import requests
from bs4 import BeautifulSoup

# 发送HTTP请求获取网页内容
url = 'https://example.com'
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup解析网页内容
 soup = BeautifulSoup(html_content, 'html.parser')

# 提取需要的信息
items = soup.find_all('div', class_='item')
for item in items:
    title = item.find('h2').text
    link = item.find('a')['href']
    print(title, link)

注意,上述代码中使用了第三方库requestsbeautifulsoup4,请确保已经安装这些库。可以在 PyCharm 的 Terminal 中使用以下命令安装:

pip install requests
pip install beautifulsoup4
PyCharm 简单爬虫代码示例 - 使用 requests 和 BeautifulSoup

原文地址: https://www.cveoy.top/t/topic/nvc8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录