实验二 网络爬虫初级实践
基本题一 显示影片基本信息
一、实验目的与要求
1、理解网络爬虫相关概念及执行流程;
2、熟练使用requests库、bs4库中的常用方法;
3、掌握独立编写爬虫程序并获取所需信息的能力。
二、实验题
访问豆瓣电影Top250(豆瓣电影 Top 250 ),获取每部电影的中文片名、排名、评分及其对应的链接,按照“排名-中文片名-评分-链接”的格式显示在屏幕上。
说明:
本实验中使用CSS选择器来获取标签,在此演示如何直接在浏览器获取指定元素的CSS选择器。
打开豆瓣电影Top250网页豆瓣电影 Top 250 在网页上右击我们所要获取的信息;
例如:要获取《肖申克的救赎》电影的导演,将鼠标移至该电影的导演处,右击鼠标出现。
点击检查,即可定位该信息在html网页源码的具体位置;
右击对应标签,选择Copy -> Copy selector,即可获得对应元素的CSS选择器。
将复制得到的CSS选择器粘贴在soup.select()中即可。
三、实验代码
import requests
import time
from bs4 import BeautifulSoup
url="https://movie.douban.com/top250?start=0"
for i in range(10):
url="https://movie.douban.com/top250?start="+ str(i*25)
print(url)
headers={
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
,'Cookie' : 'bid=uF9ux7mK05s; __utmc=30149280; push_noty_num=0; push_doumail_num=0; __gads=ID=a70f4f7daef34c82-22d73b4c7cdc002a:T=1679378544:RT=1679378544:S=ALNI_MbvrIegly9aVzyyg1KUfkj_U8zZNg; __gpi=UID=00000bdc91487ff0:T=1679378544:RT=1679378544:S=ALNI_Mbw-cobhGE0mj6qOVNYu0PLUW6srg; __yadk_uid=vRYo5bQXwbmKrd1enLtfPwbMCx8RdWjm; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1679384454%2C%22https%3A%2F%2Fcn.bing.com%2F%22%5D; _pk_ses.100001.8cb4=*; ll="118281"; __utma=30149280.1339882974.1679378543.1679378543.1679384467.2; __utmz=30149280.1679384467.2.2.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmt=1; __utmb=30149280.1.10.1679384467; dbcl2="268997119:qj6AwkV9IWg"; ck=sfDP; _pk_id.100001.8cb4=2aa0c079c9e490d3.1679379069.2.1679384535.1679379069.'
}
data=requests.get(url,headers=headers).content
soup = BeautifulSoup(data,'lxml')
name = soup.select('#content > div > div.article > ol > li > div > div.info > div.hd > a > span:nth-child(1)')
rank=soup.select("#content > div > div.article > ol > li > div > div.pic > em")
pingfen=soup.select('#content > div > div.article > ol > li > div > div.info > div.bd > div > span.rating_num')
web=soup.select('#content > div > div.article > ol > li > div > div.info > div.hd > a')
names_url=[]
for i in range(len(name)):
temp= str(rank[i].get_text())+"--"+str(name[i].get_text())+"--"+str(pingfen[i].get_text())+"--"+web[i]['href']
print(str(temp))
names_url.append(temp)
print(names_url)
基本题二 Scrapy 框架实践
一、实验目的与要求
1、理解网络爬虫相关概念及执行流程;
2、熟练使用Scrapy框架;
3、掌握独立编写爬虫程序并获取所需信息的能力。
二、实验题
访问豆瓣电影Top250(豆瓣电影 Top 250 ),在问题1的基础上,使用Srapy框架获取每部电影的导演、编剧、主演、类型、上映时间、片长、评分人数以及剧情简介等信息,并将获取到的信息保存至本地文件中。
说明:
本实验与第一个实验类似,但又有不同,需要处理子网页。第一个实验采用CSS选择器获取标签,本实验通过观察网页源码发现,主页电影信息全在<div class =" item ">…</div> 标签内,而且不同信息的 class 不同;子网页电影详细信息全在 <div id="info">…</div> 中。所以为了简化代码,本实验 使用Srapy框架获取相应的标签。
三、实验代码
import scrapy from ..items import DouBanprojectItem class DoubanprojectSpider (scrapy.Spider): name = "DouBanProject" allowed_domains = [ "movie.douban.cn" ] pre_urls = [ "http://movie.douban.com/" ] Second_urls = [ "https://movie.douban.com/subject/1292052/" ] index = 25 pre_url = 'https://movie.douban.com/top250?start=' headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' , 'Cookie' : 'bid=dJvnLS9LSZw; dbcl2="268997517:DxIMwxecuoM"; ck=_Gt4; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1679986418%2C%22https%3A%2F%2Faccounts.douban.com%2Fpassport%2Flogin%3Fredir%3Dhttps%253A%252F%252Fmovie.douban.com%252Ftop250%253Fstart%253D0%22%5D; _pk_id.100001.4cf6=0c504467b6cba249.1679986418.1.1679986418.1679986418.; __utma=30149280.314202744.1679986418.1679986418.1679986418.1; __utmc=30149280; __utmz=30149280.1679986418.1.1.utmcsr=accounts.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/passport/login; __utma=223695111.1488601765.1679986418.1679986418.1679986418.1; __utmc=223695111; __utmz=223695111.1679986418.1.1.utmcsr=accounts.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/passport/login; push_noty_num=0; push_doumail_num=0; __yadk_uid=7XpTBmn419zZXYVbR23RXwxFnykicX94; __gads=ID=d32f3926a8359ffe-221cff7fc4dc00c8:T=1679986420:RT=1679986420:S=ALNI_MZTs_yGwuU--7NrVfPYG23h3f0Pdw; __gpi=UID=00000be25238883c:T=1679986420:RT=1679986420:S=ALNI_MY88r1HJI5u3zUtk_lMeFw4_aa2Lg' } def start_requests ( self ): new_url = self .pre_url + str ( 0 ) yield scrapy.Request( url =new_url, headers = self .headers, callback = self .parse) def parse ( self , response): for element in response.xpath( '//*[ @ id = "content"]/div/div[1]/ol/li' ): no = element.xpath( './div/div[1]/em/text()' ).get() name = element.xpath( './div/div[2]/div[1]/a/span[1]/text()' ).get() grade = element.xpath( './div/div[2]/div[2]/div/span[2]/text()' ).get() new_url = element.xpath( './div/div[2]/div[1]/a/@href' ).get() # print(no, name, grade) yield scrapy.Request( url =new_url, headers = self .headers, callback = self .movie) if self .index < 250 : new_url = self .pre_url + str ( self .index) # https: // movie.douban.com / top250?start = 27 self .index += 25 yield scrapy.Request( url =new_url, headers = self .headers, callback = self .parse) # def Second_requests(self): # # two_url = self.pre1_url + str(0) # yield scrapy.Request(url=two_url, headers=self.headers, callback=self.movie) def movie ( self , response): # response.xpath('//*[@id="content"]/h1/span[1]'): daoyan = response.xpath( '//*[@id="info"]/span[1]/span[2]/a/text()' ).get() biaoju = response.xpath( '//*[@id="info"]/span[2]/span[2]/a/text()' ).get() zhuyan = response.xpath( '//*[@id="info"]/span[3]/span[2]/span/a/text()' ).get() leixing = response.xpath( '//*[@id="info"]/span/text()' ).get() shangyintime = response.xpath( '//*[@id="info"]/span[10]/text()' ).get() pianchang = response.xpath( '//*[@id="info"]/span[13]/text()' ).get() pingfenRshu = response.xpath( '//*[@id="interest_sectl"]/div[1]/div[2]/div/div[2]/a/text()' ).get() juqingjiejia = response.xpath( '//*[@id="link-report-intra"]/span[1]/span/text()' ).get() print (daoyan, biaoju, zhuyan, leixing, shangyintime, pianchang, pingfenRshu, juqingjiejia) item = DouBanprojectItem() # item['no'] = no # item['name'] = name item[ 'daoyan' ] = daoyan item[ 'biaoju' ] = biaoju item[ 'zhuyan' ] = zhuyan item[ 'shangyintime' ] = shangyintime item[ 'pianchang' ] = pianchang item[ 'pianchang' ] = pianchang item[ 'pingfenRshu' ] = pingfenRshu item[ 'juqingjiejia' ] = juqingjiejia yield item