来爬取百度首页的标题和链接:

import requests
from bs4 import BeautifulSoup

url = 'https://www.baidu.com/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

for link in soup.find_all('a'):
    print(link.get('href'), link.text)

这段代码首先使用requests库获取百度首页的HTML内容,然后使用BeautifulSoup库解析HTML内容。接着使用find_all()方法查找所有的a标签,再使用get()方法获取链接和text属性获取文本内容,最后将结果打印出来。


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

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