爬取目标
根据数据库中的豆瓣id,爬取豆瓣详情页,需要的信息是IMDb的id和该电影的简介和详情
包括source, sourceId, name, nameFrn,year,cover,directors,writers, stars, types, country,language, releaseDate, runtime, imdb, summary,rating, rateNum
使用的库
爬虫部分主要用python的requests_html库实现,使用fake_useragent库配置User Agent。
数据库部分使用pymongo库连接MongoDB并进行查询和插入操作。
import requests_html
from pymongo import MongoClient
from fake_useragent import UserAgent
连接数据库
从setting.json中读取MongoDB的host、用户名和密码
获取movie库中的profile和details集合
with open('setting.json') as f:
setting = json.load(f)
clint=MongoClient("mongodb://{}:27017/movie".format(setting['host']),username=setting['username'],password=setting['password'])
db=clint["movie"]
profile=db["profile"]
details=db["details"]
爬虫配置
ua = UserAgent(use_cache_server=False)
session = requests_html.HTMLSession()
session.mount('http://', HTTPAdapter(max_retries=3))
session.mount('https://', HTTPAdapter(max_retries=3))
session.keep_alive = False
爬取过程
从profile集合中取出目标电影详情页的url
docs=profile.find({
"source":"douban"