day03 爬取豌豆荚
from bs4 import BeautifulSoup import requests #请求url https://www.wandoujia.com/category/6001 #请求方式: get def have_title(tag): if tag.name == 'span' and tag.has_attr("title"): return tag #获取网页 def get_page(url): index_res = requests.get(url) return index_res #解析网页 def parse_detail(html): soup = BeautifulSoup(html,'lxml') list = soup.find_all(name='li',class_='card') data = "" for i in list: app_name = i.a.img.attrs['alt'] detail_url = i.a.attrs['href'] download_num = i.find(name='div',class_='meta').find(class_='install-count').text app_size = i.find(name='div',class_='meta').find(have_title).text data += f""" 名称 : {app_name} 详情页url : {detail_url} 下载人数 : {download_num} app大小 : {app_size} """ return data #保存数据 def save_games(data): with open('games.txt','w',encoding='utf-8') as f: f.write(data) if __name__ == '__main__': url = 'https://www.wandoujia.com/category/6001' index_res = requests.get(url) index_detail = index_res.text data = parse_detail(index_detail) save_games(data)