头歌Beautifulsoup解析网页

谭翔
2023-12-01
import requests
from bs4 import BeautifulSoup
def get_data(url, headers):
    '''
    两个参数
    :param url:统一资源定位符,请求网址
    :param headers:请求头
    :return data:list类型的所有古诗内容
    '''
    # ***************** Begin ******************** #
    response = requests.get(url, headers=headers)  
    response.encoding = "utf-8"  
    html = response.text  
    soup = BeautifulSoup(html, 'lxml')  
    data = soup.find('div', {'class': 'left'}).ul.find_all('li')  
    data = [i.p.text for i in data]
    # ****************** end ********************* #
    return data

 类似资料: