下面是一个使用Python编写的简单网页爬虫代码,可以输入页面的网址和需要抓取的页面标签,然后输出标签的内容:

import requests
from bs4 import BeautifulSoup

def scrape_website(url, tag):
    # 发起HTTP GET请求
    response = requests.get(url)

    # 使用BeautifulSoup解析HTML内容
    soup = BeautifulSoup(response.content, 'html.parser')

    # 查找指定的标签
    elements = soup.find_all(tag)

    # 输出标签内容
    for element in elements:
        print(element.text)

# 输入网址和标签
url = input("请输入网址: ")
tag = input("请输入需要抓取的页面标签: ")

# 调用函数进行抓取和输出
scrape_website(url, tag)

请注意,该代码使用了requests库来发起HTTP请求,需要使用pip install requests进行安装。另外,还使用了BeautifulSoup库来解析HTML内容,需要使用pip install beautifulsoup4进行安装。

使用时,运行代码后会提示输入网址和需要抓取的页面标签,例如:

请输入网址: https://example.com
请输入需要抓取的页面标签: h1

然后,程序会输出指定标签的内容。在上面的例子中,如果页面中存在<h1>标签,它的内容将被输出

使用python语言编写网页爬虫代码输入页面的网址及需要抓取的页面标签输出标签的内容

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

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