3.1.豆瓣電影
使用lxml
import requests from lxml import etree headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36', 'Referer':'https://movie.douban.com/' } url = 'https://movie.douban.com/cinema/nowplaying/beijing/' response = requests.get(url,headers=headers) text = response.text html = etree.HTML(text) #獲取正在上映的電影 ul = html.xpath("//ul[@class='lists']")[0] lis = ul.xpath("./li") movies = [] for li in lis: title = li.xpath("@data-title")[0] score = li.xpath("@data-score")[0] duration = li.xpath("@data-duration")[0] region = li.xpath("@data-region")[0] director = li.xpath("@data-director")[0] actors = li.xpath("@data-actors")[0] #電影海報圖片 thumbnail = li.xpath(".//img/@src")[0] movie = { 'title':title, 'score':score, 'duration':duration, 'region':region, 'director':director, 'actors':actors, 'thumbnail':thumbnail, } movies.append(movie) print(movies)
3.2.電影天堂
使用lxml
import requests from lxml import etree BASE_DOMAIN = 'http://dytt8.net' HEADERS = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36', } def get_detail_urls(url): '''獲取詳情頁url''' response = requests.get(url, headers=HEADERS) text = response.text html = etree.HTML(text) # 獲取電影詳情的url detail_urls = html.xpath("//table[@class='tbspan']//a/@href") #加上域名,拼接成完整的url detail_urls = map(lambda url:BASE_DOMAIN + url,detail_urls) return detail_urls def parse_detail_page(url): '''處理爬取頁面''' movie = {} response= requests.get(url,headers=HEADERS) text = response.content.decode('gbk') html = etree.HTML(text) title = html.xpath("//div[@class='title_all']//font[@color='#07519a']/text()")[0] movie['title'] = title zoomE = html.xpath("//div[@id='Zoom']")[0] imgs = zoomE.xpath(".//img/@src") cover = imgs[0] #電影海報 movie['cover'] = cover #因為有的電影沒有截圖,所有這里加個異常處理 try: screenshot = imgs[1] #電影截圖 movie['screenshot'] = screenshot except IndexError: pass infos = zoomE.xpath(".//text()") for index,info in enumerate(infos): if info.startswith("◎年 代"): info = info.replace("◎年 代","").strip() movie['year'] = info elif info.startswith("◎產 地"): info = info.replace("◎產 地", "").strip() movie['country'] = info elif info.startswith("◎類 別"): info = info.replace("◎類 別", "").strip() movie['category'] = info elif info.startswith("◎豆瓣評分"): info = info.replace("◎豆瓣評分", "").strip() movie['douban_rating'] = info elif info.startswith("◎片 長"): info = info.replace("◎片 長", "").strip() movie['duration'] = info elif info.startswith("◎導 演"): info = info.replace("◎導 演", "").strip() movie['director'] = info #影片的主演有多個,所有要添加判斷 elif info.startswith("◎主 演"): info = info.replace("◎主 演", "").strip() actors = [info,] for x in range(index+1,len(infos)): actor = infos[x].strip() if actor.startswith("◎簡 介 "): break actors.append(actor) # print(actors) movie['actors'] = actors elif info.startswith("◎簡 介 "): info = info.replace("◎簡 介 ", "").strip() for x in range(index+1,len(infos)): profile = infos[x].strip() if profile.startswith("【下載地址】"): break # print(profile) movie['profile'] = profile #下載地址 download_url = html.xpath("//td[@bgcolor='#fdfddf']/a/@href")[0] # print(download_url) movie['download_url'] = download_url return movie def spider(): base_url = 'http://dytt8.net/html/gndy/dyzz/list_23_{}.html' movies = [] for x in range(1,8): #爬前7頁 url = base_url.format(x) detail_urls = get_detail_urls(url) for detail_url in detail_urls: movie = parse_detail_page(detail_url) movies.append(movie) print(movie) print(movies) #所有的電影信息 if __name__ == '__main__': spider()