Open yunfengsay opened 4 years ago
# 不提pr了... 先在issues里看吧 # exercise-for-python ''' 1.选取需要的Url;(豆瓣上我感兴趣的话题?) 2.使用Requests库请求网站; 3.使用Get方式抓取数据; 4.最后把抓取数据导入文本。 ''' # -*- coding: utf-8 -*- from urllib import request import requests from bs4 import BeautifulSoup #找到豆瓣发现小组的网页 url='https://www.douban.com/group/explore?tag=%E5%85%B4%E8%B6%A3' #设置请求头 headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36' } response = requests.get(url,headers=headers) strhtml=requests.get(url,headers=headers) soup=BeautifulSoup(strhtml.text,'lxml') #定位所需数据的路径并引用 #content > div > div.article > div.group-list > div:nth-child(17) > div.content > div.title > h3 > a # > 是子选择符号, 改成下面这种后代选择就可以少写几个条件 data=soup.select('div.group-list div.title a') #提取数据并导出 result = [] for item in data : # 不要再for循环里反复的打开文件,这样很耗io时间 result.append({ 'title':item.get_text(), 'link':item.get('href') }) # 使用 with open('douban_interesting_spider.txt', 'w') as f: f.write(str(result)) ''' 建议 1. 爬取所有豆瓣小组不是就第一页的 2. 使用函数封装逻辑, 比如抽象出 makeQueryheader, queryDoubanDom, paseData, save2file 3. 储存下来后本地建立一个 http 的server 提供接口服务,使得访问该接口可以得到本地保存的文件 4. 建立一个简单的网页请求第三步创建的接口重新实现一个豆瓣兴趣小组 '''
Suggests received.✧(●'◡'●)Thanks for helping!