![c90cd075-6a13-eb11-8da9-e4434bdf6706.gif](http://p02.5ceimg.com/content/c90cd075-6a13-eb11-8da9-e4434bdf6706.gif)
![cd0cd075-6a13-eb11-8da9-e4434bdf6706.jpeg](http://p02.5ceimg.com/content/cd0cd075-6a13-eb11-8da9-e4434bdf6706.jpeg)
出品 | CSDN(ID:CSDNnews)
本文章精选了五个爬虫实例,希望能够给想要入门 Python 爬虫的小伙伴儿们一些帮助。![d30cd075-6a13-eb11-8da9-e4434bdf6706.png](http://p02.5ceimg.com/content/d30cd075-6a13-eb11-8da9-e4434bdf6706.png)
Python
def search_keyword(keyword):
uri = 'https://you.163.com/xhr/search/search.json'
query = {
"keyword": keyword,
"page": 1
}
try:
res = requests.get(uri, params=query).json()
result = res['data']['directly']['searcherResult']['result']
product_id = []
for r in result:
product_id.append(r['id'])
return product_id
except:
raise
我这里是获取了 page 为 1 的产品 ID,下面就是通过产品 ID 来获取不同产品下的评论信息。
通过前面的分析,我们可以知道,评论信息都是如下形式的,对这种形式的信息,我们可以很方便地存储进入 MongoDB,然后再慢慢分析数据里的内容。{
"skuInfo": [
"机型:iphone X",
"颜色:透明白"
],
"frontUserName": "c****x",
"frontUserAvatar": "https://yanxuan.nosdn.127.net/c230f2c2a6e7223810755518ce5cd62f",
"content": "手机套很薄,裸机的手感,硅胶的摸着好舒服,看着屏幕都变大了。颜值很高。",
"createTime": 1554982110981,
"picList": [
"https://yanxuan.nosdn.127.net/749cfcb4877be8e7fce449900d763731.jpg",
"https://yanxuan.nosdn.127.net/6744d04efcd7509556b710bc0d9fa6b0.jpg"
],
"commentReplyVO": null,
"memberLevel": 4,
"appendCommentVO": null,
"star": 5,
"itemId": 3444035
}
对于 MongoDB,我们既可以自己搭建,也可以使用网上免费的服务。在这里我介绍一个免费的 MongoDB 服务网站:mlab,使用很简单,就不过多介绍使用过程了。
数据库有了,下面就是把数据保存进去了。
def details(product_id):
url = 'https://you.163.com/xhr/comment/listByItemByTag.json'
try:
C_list = []
for i in range(1, 100):
query = {
"itemId": product_id,
"page": i,
}
res = requests.get(url, params=query).json()
if not res['data']['commentList']:
break
print("爬取第 %s 页评论" % i)
commentList = res['data']['commentList']
C_list.append(commentList)
time.sleep(1)
# save to mongoDB
try:
mongo_collection.insert_many(commentList)
except:
continue
return C_list
except:
raise
在这里,details 这个函数还返回了评论的列表,如果你还有其他的逻辑需要对评论处理,可以接收这个函数的返回值,然后处理该列表。
最后爬取完成之后,总共是七千多条数据,下面就可以根据个人需要做一些分析了。
conn = MongoClient("mongodb://%s:%s@ds149974.mlab.com:49974/you163" % ('you163', 'you163'))
db = conn.you163
mongo_collection = db.iPhone
Boss 直聘网站爬取
其实分析的步骤都是类似的,无外乎是先分析网站信息,页面组成,然后编写代码爬取数据,最后再保存数据。 页面分析 我们选取一个岗位来分析,比如:Python。 在 Boss 直聘的官网上搜索 Python,可以看到浏览器的 URL 变为如下:from bs4 import BeautifulSoup
import requests
url = 'https://www.zhipin.com/job_detail/?query=python&city=101010100'
res = requests.get(url, headers=header).text
print(res)
content = BeautifulSoup(res, "html.parser")
ul = content.find_all('ul')
print(ul[12])
可以使用 BeautifulSoup 的 find 函数来查找 HTML 的元素标签,个人觉得还是挺方便的。
编写代码
我们通过分析 HTML 网页可以知道,所有的工作信息都是保存在 ul 这个标签中的,我们可以通过上面的代码拿到页面中所有的 ul 标签,find_all 返回的是一个列表,然后再查看,工作具体位于第几个 ul 中,这样就拿到具体的工作信息了。
确定需要抓取的数据
Python:可以得到该 job 具体页面地址
10-15K:每个 job 的薪资
柯莱特集团:招聘公司名称
北京 朝阳区 望京|3-5年|学历不限:该 job 的详情信息
job_details_uri = job.find('h3', attrs={'class': 'name'}).find('a')['href']
job_company = job.find('div', attrs={'class': 'company-text'}).find('h3', attrs={'class': 'name'}).find('a').text
job_salary = job.find('h3', attrs={'class': 'name'}).find('span', attrs={'class': 'red'}).text
对于 job 的详情信息,需要用到正则表达式来分割字符串,我们先来看下该部分的原始形式:
rege = r'
([\u4e00-\u9fa5 ]+)([\d+-年]+|[\u4e00-\u9fa5]+)([\u4e00-\u9fa5]+)'
正则表达式的具体写法这里就不说了,不熟悉的可以自行查找下。
下面把这部分代码整合到一起:for job in jobs:
job_dict = {}
job_details_uri = job.find('h3', attrs={'class': 'name'}).find('a')['href']
job_company = job.find('div', attrs={'class': 'company-text'}).find('h3', attrs={'class': 'name'}).find('a').text
job_salary = job.find('h3', attrs={'class': 'name'}).find('span', attrs={'class': 'red'}).text
job_details = str(job.find('p'))
print(job_details)
job_rege = re.match(rege, job_details)
job_dict['name'] = job_company
job_dict['uri'] = job_details_uri
job_dict['salary'] = job_salary
job_dict['site'] = job_rege.group(1)
job_dict['year'] = job_rege.group(2)
job_dict['edu'] = job_rege.group(3)
job_list.append(job_dict)
print(job_list)
由于我们最后还是得到了一个列表,里面包含字典,同样可以快捷的保存到 MongoDB 中。
抓取多个页面
通过查看 Boss 网站的下一页源码可得到翻页 URL 的规律:
https://www.zhipin.com/c101010100/?query=python&page=
c101010100:是城市代码,在我们这里就代表北京
query:也很明显,就是我们的搜索关键字
page:页数
![f90cd075-6a13-eb11-8da9-e4434bdf6706.png](http://p02.5ceimg.com/content/f90cd075-6a13-eb11-8da9-e4434bdf6706.png)
def jobs(page):
for i in range(1, page + 1):
job_list = []
try:
print("正在抓取第 %s 页数据" % i)
uri = '/c101010100/?query=python&page=%s' % i
res = requests.get(config.url + uri, headers=header).text
content = BeautifulSoup(res, "html.parser")
ul = content.find_all('ul')
jobs = ul[12].find_all("li")
...
print(job_list)
# save to mongoDB
try:
mongo_collection.insert_many(job_list)
except:
continue
time.sleep(1)
except:
continue
因为我上面的正在表达式并不能匹配所有的情况,所以使用 try...except 来忽略了其他不规则的情况。
岗位详情抓取
job 详情抓取完毕之后,开始抓取岗位详情,就是每个 job 的具体要求,毕竟知己知彼,百战不殆。
我们可以从 URI 中获得每个工作的详情页面地址,然后再拼接到 Boss 的主 URL 上:
https://www.zhipin.com/job_detail/a8920821a7487a901HJ43tm7EFY~.html再来看下工作详情页面,所有的任职描述都在如下的 div 标签中:
![fa0cd075-6a13-eb11-8da9-e4434bdf6706.png](http://p04.5ceimg.com/content/fa0cd075-6a13-eb11-8da9-e4434bdf6706.png)
job_conn = MongoClient("mongodb://%s:%s@ds151612.mlab.com:51612/boss" % ('boss', 'boss123'))
job_db = job_conn.boss
job_collection = job_db.boss
details_collection = job_db.job_details
def run_main():
jobs = job_collection.find()
for job in jobs:
print('获得工作的uri ', job['uri'])
get_details(job)
time.sleep(1)
def get_details(items):
base_url = config.url
url = base_url + items['uri']
company_name = items['name']
try:
res = requests.get(url, headers=header).text
content = BeautifulSoup(res, "html.parser")
text = content.find('div', attrs={'class': 'text'}).text.strip()
result = {'name': company_name, 'details': text}
details_collection.insert_one(result)
except:
raise
if __name__ == '__main__':
run_main()
注意下这里的 MongoDB 配置,是同一个 db 下的不同 collection。这样,也就完成了直聘网站相关岗位的数据爬取。
完整代码地址:https://github.com/zhouwei713/dataanalysis/tree/master/bossspider。
爬取的数据 MongoDB 链接:
job_conn = MongoClient("mongodb://%s:%s@ds151612.mlab.com:51612/boss" % ('boss', 'boss123'))
job_db = job_conn.boss
job_collection = job_db.boss
details_collection = job_db.job_details
![fc0cd075-6a13-eb11-8da9-e4434bdf6706.png](http://p05.5ceimg.com/content/fc0cd075-6a13-eb11-8da9-e4434bdf6706.png)
s.weibo.com/user?q=林志玲
同样是先放到 Postman 里请求下,看看能不能直接访问:
def get_uid(name):
try:
url = 'https://s.weibo.com/user?q=%s' % name
res = requests.get(url).text
content = BeautifulSoup(res, 'html.parser')
user = content.find('div', attrs={'class': 'card card-user-b s-pg16 s-brt1'})
user_info = user.find('div', attrs={'class': 'info'}).find('div')
href_list = user_info.find_all('a')
if len(href_list) == 3:
title = href_list[1].get('title')
if title == '微博个人认证':
uid = href_list[2].get('uid')
return uid
elif title == '微博会员':
uid = href_list[2].get('uid')
return uid
else:
print("There are something wrong")
return False
except:
raise
还是通过 BeautifulSoup 来定位获取元素,最后返回 UID 信息。
M 站的利用
M 站一般是指手机网页端的页面,也就是为了适配 mobile 移动端而制作的页面。一般的网站都是在原网址前面加“m.”来作为自己 M 站的地址,比如:m.baidu.com 就是百度的 M 站。
我们来打开微博的 M 站,再进入到林志玲的微博页面看看 Network 中的请求,有没有什么惊喜呢?
我们首先发现了这样一个 URL:
https://m.weibo.cn/api/container/getIndex?uid=1312412824&luicode=10000011&lfid=100103type%3D1%26q%3D%E6%9E%97%E5%BF%97%E7%8E%B2&containerid=1005051312412824接着继续拖动网页,发现 Network 中又有类似的 URL:
https://m.weibo.cn/api/container/getIndex?uid=1312412824&luicode=10000011&lfid=100103type%3D1%26q%3D%E6%9E%97%E5%BF%97%E7%8E%B2&containerid=1076031312412824URL 类似,但是第一个返回的数据是用户信息,而第二个返回的则是用户的微博信息,显然第二个 URL 是我们需要的。同样道理,把第二个 URL 放到 Postman 中,看看哪些参数是可以省略的。
def get_userinfo(uid):
try:
url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=%s' % uid
res = requests.get(url).json()
containerid = res['data']['tabsInfo']['tabs'][1]['containerid']
mblog_counts = res['data']['userInfo']['statuses_count']
followers_count = res['data']['userInfo']['followers_count']
userinfo = {
"containerid": containerid,
"mblog_counts": mblog_counts,
"followers_count": followers_count
}
return userinfo
except:
raise
代码里都是基本操作,不过多解释了。
拿到 containerid 信息之后,我们就可以使用上面第二个 URL 来获取微博信息了,这里还是同样的问题——分页。怎么处理分页呢,继续改造这个 getIndex 接口,继续尝试传递不同的参数给它。
这次给它传递 containerid 和 page 信息,就可以完成分页请求了。
def get_blog_info(cards, i, name, page):
blog_dict = {}
if cards[i]['card_type'] == 9:
scheme = cards[i]['scheme'] # 微博地址
mblog = cards[i]['mblog']
mblog_text = mblog['text']
create_time = mblog['created_at']
mblog_id = mblog['id']
reposts_count = mblog['reposts_count'] # 转发数量
comments_count = mblog['comments_count'] # 评论数量
attitudes_count = mblog['attitudes_count'] # 点赞数量
with open(name, 'a', encoding='utf-8') as f:
f.write("----第" + str(page) + "页,第" + str(i + 1) + "条微博----" + "\n")
f.write("微博地址:" + str(scheme) + "\n" + "发布时间:" + str(create_time) + "\n"
+ "微博内容:" + mblog_text + "\n" + "点赞数:" + str(attitudes_count) + "\n"
+ "评论数:" + str(comments_count) + "\n" + "转发数:" + str(reposts_count) + "\n")
blog_dict['mblog_id'] = mblog_id
blog_dict['mblog_text'] = mblog_text
blog_dict['create_time'] = create_time
return blog_dict
else:
print("没有任何微博哦")
return False
函数参数:
第一个参数,接受的值为 res['data']['cards'] 的返回值,是一个字典类型数据;
第二个参数,是外层调用函数的循环计数器;
第三个参数,是要爬取的大 V 名称;
第四个参数,是正在爬取的页码。
def get_blog_by_text(containerid, blog_text, name):
blog_list = []
page = 1
while True:
try:
url = 'https://m.weibo.cn/api/container/getIndex?containerid=%s&page=%s' % (containerid, page)
res_code = requests.get(url).status_code
if res_code == 418:
print("访问太频繁,过会再试试吧")
return False
res = requests.get(url).json()
cards = res['data']['cards']
if len(cards) > 0:
for i in range(len(cards)):
print("-----正在爬取第" + str(page) + "页,第" + str(i+1) + "条微博------")
blog_dict = get_blog_info(cards, i, name, page)
blog_list.append(blog_dict)
if blog_list is False:
break
mblog_text = blog_dict['mblog_text']
create_time = blog_dict['create_time']
if blog_text in mblog_text:
print("找到相关微博")
return blog_dict['mblog_id']
elif checkTime(create_time, config.day) is False:
print("没有找到相关微博")
return blog_list
page += 1
time.sleep(config.sleep_time)
else:
print("没有任何微博哦")
break
except:
pass
这里调用了一个工具函数 checkTime 和一个配置文件 config。
checkTime 函数定义如下:
def checkTime(inputtime, day):
try:
intime = datetime.datetime.strptime("2019-" + inputtime, '%Y-%m-%d')
except:
return "时间转换失败"
now = datetime.datetime.now()
n_days = now - intime
days = n_days.days
if days return True
else:
return False
定义这个函数的目的是为了限制搜索时间,比如对于 90 天以前的微博,就不再搜索了,也是提高效率。
而 config 配置文件里,则定义了一个配置项 day,来控制可以搜索的时间范围:
day = 90 # 最久抓取的微博时间,60即为只抓取两个月前到现在的微博
sleep_time = 5 # 延迟时间,建议配置5-10s
获取评论信息
对于微博评论信息的获取,要简单很多。
我们进入某一个微博页面,进入到评论区:
https://weibo.com/1312412824/HxFY84Gqb?filter=hot&root_comment_id=0&type=comment#_rnd1567155548217
https://weibo.com/aj/v6/comment/big?ajwvr=6&id=4380261561116383&from=singleWeiBo&__rnd=1567155729639同样使用 Postman 进行 URL 精简和分页处理,可以得到最后的 URL 为:
https://weibo.com/aj/v6/comment/big?ajwvr=6&id=%s&page=%s
id 就是要抓取评论的微博对应的 id,我们已经在上面的接口中拿到了;
page 就是请求页数。
def get_comment(self, mblog_id, page):
comment = []
for i in range(0, page):
print("-----正在爬取第" + str(i) + "页评论")
url = 'https://weibo.com/aj/v6/comment/big?ajwvr=6&id=%s&page=%s' % (mblog_id, i)
req = requests.get(url, headers=self.headers).text
html = json.loads(req)['data']['html']
content = BeautifulSoup(html, "html.parser")
comment_text = content.find_all('div', attrs={'class': 'WB_text'})
for c in comment_text:
_text = c.text.split(":")[1]
comment.append(_text)
time.sleep(config.sleep_time)
return comment
def download_comment(self, comment):
comment_pd = pd.DataFrame(columns=['comment'], data=comment)
timestamp = str(int(time.time()))
comment_pd.to_csv(timestamp + 'comment.csv', encoding='utf-8')
定义运行函数
最后,我们开始定义运行函数,把需要用户输入的相关信息都从运行函数中获取并传递给后面的逻辑函数中。
from weibo_spider import WeiBo
from config import headers
def main(name, spider_type, text, page, iscomment, comment_page):
print("开始...")
weibo = WeiBo(name, headers)
...
if __name__ == '__main__':
target_name = input("type the name: ")
spider_type = input("type spider type(Text or Page): ")
text = "你好"
page_count = 10
iscomment = "No"
comment_page_count = 100
while spider_type not in ("Text", "text", "Page", "page"):
spider_type = input("type spider type(Text or Page): ")
...
通过 input 函数接受用户输入信息,再判断程序执行。
爬虫类与工具集
最后再来看下程序中的 WeiBo 爬虫类的定义:
class WeiBo(object):
def __init__(self, name, headers):
self.name = name
self.headers = headers
def get_uid(self): # 获取用户的 UID
...
def get_userinfo(self, uid): # 获取用户信息,包括 containerid
...
def get_blog_by_page(self, containerid, page, name): # 获取 page 页的微博信息
...
def get_blog_by_text(self, containerid, blog_text, name): # 一个简单的搜索功能,根据输入的内容查找对应的微博
...
def get_comment(self, mblog_id, page): # 与上个函数配合使用,用于获取某个微博的评论
...
def download_comment(self, comment): # 下载评论
...
在类的初始化函数中,传入需要爬取的大 V 名称和我们准备好的 headers(cookie),然后把上面写好的函数写道该类下,后面该类的实例 weibo 就能够调用这些函数了。
对于工具集,就是抽象出来的一些逻辑处理:
import datetime
from config import day
def checkTime(inputtime, day):
...
def get_blog_info(cards, i, name, page):
...
最终程序运行示例:
![240dd075-6a13-eb11-8da9-e4434bdf6706.png](http://p04.5ceimg.com/content/240dd075-6a13-eb11-8da9-e4434bdf6706.png)
http://api.dongqiudi.com/search?keywords=&type=all&page=我们可以通过给 keyword 传入“女神大会”关键字,来获取到女神大会相关的信息:
id 是对应的每个网页的 id;
thumb 是女神的封面图片;
url 对应的也是女神所在页面的地址信息。
def get_list(page):
nvshen_id_list = []
nvshen_id_picture = []
for i in range(1, page):
print("获取第" + str(i) + "页数据")
url = 'http://api.dongqiudi.com/search?keywords=%E5%A5%B3%E7%A5%9E%E5%A4%A7%E4%BC%9A&type=all&page=' + str(i)
html = requests.get(url=url).text
news = json.loads(html)['news']
if len(news) == 0:
print("没有更多啦")
break
nvshen_id = [k['id'] for k in news]
nvshen_id_list = nvshen_id_list + nvshen_id
nvshen_id_picture = nvshen_id_picture + [{k['id']: k['thumb']} for k in news]
time.sleep(1)
return nvshen_id_list, nvshen_id_picture
下载 HTML 页面
我们把上面 API 拿到的 url 字段放到懂球帝的主地址下,拼接成如下 url:
http://www.dongqiudi.com/news/1193890打开该 URL,发现确实是对应的详情页面:
def download_page(nvshen_id_list):
for i in nvshen_id_list:
print("正在下载ID为" + i + "的HTML网页")
url = 'https://www.dongqiudi.com/news/%s' % i
download = DownloadPage()
html = download.getHtml(url)
download.saveHtml(i, html)
time.sleep(2)
class DownloadPage(object):
def getHtml(self, url):
html = requests.get(url=url, cookies=config.session, headers=config.header).content
return html
def saveHtml(self, file_name, file_content):
with open('html_page/' + file_name + '.html', 'wb') as f:
f.write(file_content)
download_page 函数接收一个列表(就是上一个函数的返回值 nvshen_id_list),通过 requests 库来请求页面并保存至本地。同时为了方式请求过于频繁,做了 2 秒的延时等待设置。
这里还要注意一点的是,这里是设置了 cookies 和 headers 的,否则是无法拿到正常 HTML 数据的,headers 还是从浏览器中手动拷贝出来:
session = {
'dqduid': 'yours'}
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win32; x32; rv:54.0) Gecko/20100101 Firefox/54.0',
'Connection': 'keep-alive'}
解析 HTML
对于 HTML 的解析,我依旧使用 BeautifulSoup。主要需要获取 score 信息,但是由于好多 HTML 页面写的都不是很规则,故而这里耗费了比较多的时间。
content.find_all('span', attrs={'style': "color:#ff0000"})
但是有些页面的规则不同,所以会存在无法解析一部分 HTML 文件,对于无法处理的文件,由于也不多,就手工处理了。
当然,感觉这里使用正则应该会好很多。代码过长,为了不影响阅读,就不贴出来了,可以到 GitHub 上查看完整代码。def deal_loaclfile(nvshen_id_picture):
files = os.listdir('html_page/')
nvshen_list = []
special_page = []
for f in files:
...
return nvshen_list, special_page
def get_picture(c, t_list, n_id_p):
nvshen_l = []
tmp_prev_id = c.find_all('a', attrs={"target": "_self"})
for j in tmp_prev_id:
...
有一部分 HTML 代码不是很规范,专门提出了这一部分:
w_list = ['吴宣仪', '30万', '826965', '68', '825847',
'https://img1.dongqiudi.com/fastdfs3/M00/74/54/180x135/crop/-/ChOxM1vIPpOAZT8AAAHza_WMyRk175.png']
g_list = ['关之琳', '20万', '813611', '88', '812559',
'https://img1.dongqiudi.com/fastdfs3/M00/6B/94/180x135/crop/-/ChOxM1u1gx2AZ7qmAABi3gRdHS8715.jpg']
t_list = ['佟丽娅', '22万', '797779', '93', '795697',
'https://img1.dongqiudi.com/fastdfs3/M00/60/A7/180x135/crop/-/ChOxM1ufUh2AJdR0AABXtcU22fg956.jpg']
y_list = ['杨丞琳', '7万', '1173681', '45', '1168209',
'https://img1.qunliao.info/fastdfs4/M00/CA/F7/ChMf8F0pTOKAaefqAA5nOMM0LK0171.jpg']
保存数据
对于数据的保存,直接保存在了本地的 CSV 中,代码就不贴了。
完整代码地址:https://github.com/zhouwei713/data_analysis/tree/master/nvshendahui。
展示网站地址:https://nvshen.luobodazahui.top/。
![370dd075-6a13-eb11-8da9-e4434bdf6706.png](http://p04.5ceimg.com/content/370dd075-6a13-eb11-8da9-e4434bdf6706.png)
https://movie.douban.com/celebrity/1166896/photos/?type=C&start=30&sortby=like&size=a&subtype=a继续使用 Postman 来分析 URL,可以很轻松的得知,start 就是类似于 page 的页数控制参数,而且步长为 30,即第一页是 start = 0,第二页为 start = 30,第三页为 start = 60,以此类推。 详情页分析 使用 Network 来查看页面上的图片信息:
![3c0dd075-6a13-eb11-8da9-e4434bdf6706.png](http://p04.5ceimg.com/content/3c0dd075-6a13-eb11-8da9-e4434bdf6706.png)
a 标签中的链接可以得到每张图片的评论信息;
img 标签中的链接可以用来保存女神的海报。
def get_posters():
comment_url_list = []
picture_list = []
for i in range(0, 40000, 30):
url = 'https://movie.douban.com/celebrity/1166896/photos/?type=C&start=%s&sortby=like&size=a&subtype=a' % str(i)
req = requests.get(url).text
content = BeautifulSoup(req, "html.parser")
chekc_point = content.find('span', attrs={'class': 'next'}).find('a')
if chekc_point != None:
data = content.find_all('div', attrs={'class': 'cover'})
for k in data:
ulist = k.find('a')['href']
plist = k.find('img')['src']
comment_url_list.append(ulist)
picture_list.append(plist)
else:
break
return comment_url_list, picture_list
之后,就可以下载海报了。
评论获取 然后我们手动跳转到每周海报的详情页面,继续查看评论信息。def get_comment(comment_l):
client = pymongo.MongoClient('mongodb://douban:douban1989@ds149744.mlab.com:49744/douban')
db = client.douban
mongo_collection = db.comment
comment_list = []
comment = []
print("Save to MongoDB")
for i in comment_l:
response = requests.get(i).text
content = BeautifulSoup(response, "html.parser")
tmp_list = content.find_all('div', attrs={'class': 'comment-item'})
comment_list = comment_list + tmp_list
for k in comment_list:
tmp_comment = k.find('p').text
mongo_collection.insert_one({'comment': tmp_comment})
comment.append(tmp_comment)
print("Save Finish!")
完整代码地址:https://github.com/zhouwei713/douban/tree/master/wangzuxian_poster。
爬取的数据 MongoDB 链接:
client = pymongo.MongoClient('mongodb://douban:douban1989@ds149744.mlab.com:49744/douban')
db = client.douban
mongo_collection = db.comment
![450dd075-6a13-eb11-8da9-e4434bdf6706.png](http://p05.5ceimg.com/content/450dd075-6a13-eb11-8da9-e4434bdf6706.png)
【END】
![470dd075-6a13-eb11-8da9-e4434bdf6706.jpeg](http://p01.5ceimg.com/content/470dd075-6a13-eb11-8da9-e4434bdf6706.jpeg)
热 文 推 荐
☞ 阿里开源物联网操作系统 AliOS Things 3.0 发布,集成平头哥 AI 芯片架构!☞ 余承东吐槽苹果续航; 微软 IE 浏览器被曝漏洞; React Native 0.61.0 发布 | 极客头条 ☞ Spring Boot 面试,一个问题就问趴下了!☞ 10分钟Get拥抱无服务的正确姿势☞为什么平头哥做芯片如此迅猛?
☞解决掉这些痛点和难点,让知识图谱不再是“噱头” ☞限时早鸟票 | 2019 中国大数据技术大会(BDTC)超豪华盛宴抢先看! ☞用万圣节糖果解释什么是“零知识证明”, 可能是关于密码学很通俗的解释了……![4d0dd075-6a13-eb11-8da9-e4434bdf6706.gif](http://p01.5ceimg.com/content/4d0dd075-6a13-eb11-8da9-e4434bdf6706.gif)
![530dd075-6a13-eb11-8da9-e4434bdf6706.png](http://p03.5ceimg.com/content/530dd075-6a13-eb11-8da9-e4434bdf6706.png)