以下是一个简单的Python爬虫示例,可以爬取指定网站中的文字信息,并将其输出到控制台上:

import requests
from bs4 import BeautifulSoup

url = "https://www.example.com" # 替换为你要爬取的网站地址

response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

# 查找所有的文本信息
text_list = []
for text in soup.find_all(text=True):
    if text.parent.name not in ['style', 'script', 'head', 'title', 'meta', '[document]']:
        text_list.append(text)

# 输出文本信息
for text in text_list:
    print(text.strip())

解释:

  1. 导入所需的库:requests用于获取网页内容,BeautifulSoup用于解析网页内容。
  2. 定义要爬取的网站地址。
  3. 通过requests库获取网页内容,并用BeautifulSoup解析网页内容。
  4. 查找所有的文本信息,并将其存储到一个列表中。
  5. 遍历文本信息列表,并输出每个文本信息。
python写条爬虫爬取文字信息

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

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