pyqt吧 关注:422贴子:665
  • 4回复贴,共1

现在ai真好用

只看楼主收藏回复

请帮我写个Python爬虫代码
以下是一个简单的使用requests和BeautifulSoup库来爬取网页标题的Python示例代码。首先确保你已经安装了这两个库,可使用pip install requests beautifulsoup4进行安装。
import requestsfrom bs4 import BeautifulSoup
def get_webpage_title(url): try: # 发送HTTP GET请求 response = requests.get(url) # 检查请求是否成功 response.raise_for_status() soup = BeautifulSoup(response.content, 'html.parser') # 获取网页标题 title = soup.title.string return title except requests.RequestException as e: print(f"请求出现错误: {e}") return None
if __name__ == "__main__": target_url = "https://www.example.com" # 替换为你要爬取的网址 page_title = get_webpage_title(target_url) if page_title: print(f"网页标题是: {page_title}")


IP属地:湖北来自iPhone客户端1楼2025-02-17 00:08回复


    IP属地:湖北来自iPhone客户端2楼2025-02-17 00:10
    回复
      改吧改吧可以用


      IP属地:湖北来自iPhone客户端3楼2025-02-17 00:10
      回复


        IP属地:湖北来自iPhone客户端4楼2025-02-17 00:10
        回复
          是吧


          IP属地:湖北来自iPhone客户端5楼2025-02-17 00:11
          回复