爬虫——用正则表达式以及BeautifulSoup两种方法爬取豆瓣电影TOP100

(一)正则表达式:

1.获取HTML内容:

  html=urllib.request.urlopen(url)

  html=html.read().decode('utf-8')——注意编码

2.爬取需要的信息点,提取正则表达式:

  key=re.compile(r'正则表达式')

  information=re.findall(key,html)

3.清洗处理数据,得到准确信息

  a.存在空格 ——string.strip()

  b.分割 ——string.split()

  c.存在某个有规律的符号 ——string.find()==-1

4.打印得到需要的信息点

完整的代码如下:

#encoding:utf-8
import urllib.request
import re
#获取单个电影链接里的内容,比如评分,剧情简介
def get_score_information(link):
	html=urllib.request.urlopen(link)
	html=html.read().decode("utf-8")
	score_patten=re.compile(r'<strong class=".*" property=".*">(.*)</strong>')
	score_all=re.findall(score_patten,html)
	for score in score_all:
		print ("The movie score is :"+score)

count=1
url="https://movie.douban.com/top250?start="
urls=[url+str(num*25) for num in range(4)]#列表推导式
for one in urls:
	html=urllib.request.urlopen(one)
	html=html.read().decode("utf-8")
#提取电影名称的正则表达式
	title_patten=re.compile(r'<span class="title">(.*?)</span>')
#提取电影链接内容的正式表达式
	link_patten=re.compile(r'<a href="(.*?)" class="">')
	title_all=re.findall(title_patten,html)
	link_all=re.findall(link_patten,html)
#因为得到的电影名称有其它格式的存在,需要清洗整理到新的列表
	title_arr=[]
	for each in title_all:
		if each.find('/')==-1:
			title_arr.append(each)
	for title,link in zip(title_arr,link_all):
			print ("Top "+str(count)+":	"+title+"	link:	"+link)
			get_score_information(link)
			count+=1

存在的问题是:要是需要提取多个信息点,得编写一段长度不可估的正则表达式,要不然就是分开提取正则表达式,再一一整理统一打印输出。

 

(二)BeautifulSoup

#encoding:utf-8
import requests
from bs4 import BeautifulSoup
number=1
url='https://movie.douban.com/top250?start=0'
#获取HTML内容
html=requests.get(url)
#输出文本格式自动编码
html=html.text
#实例化对象,html.parser 是解析html
soup=BeautifulSoup(html,'html.parser')
#找到爬取电影名称的标签
movie_title=soup.find_all("span",class_="title")
#清洗数据
title_arr=[]
for title in movie_title:
	if title.text.find('/')==-1:
		title_arr.append(title.text)
#注意link.a.attrs['href'] ,是找到标签下的属性,方便获取信息点
#<a href="">
movie_link=soup.find_all('div',class_="hd")
for title_one ,link in zip(title_arr,movie_link):
	print ("Top "+str(number)+"	:	"+title_one+"	movie link:	"+link.a.attrs['href'])
	number+=1

总结:BeautifulSoup可以利用标签方便获取多个信息点

 

用BeautifulSoup 进行代码重构():

 

encoding:utf-8
import requests
from bs4 import BeautifulSoup

#爬取电影链接后解析链接获取电影评分以及剧情简介
def get_information(link):
	html=requests.get(link)
	soup=BeautifulSoup(html.text,'html.parser')
	movie_score=soup.find_all("strong",class_="ll rating_num")
	for score in movie_score:	
		print ("The movie score is :		"+score.text)
	movie_detail=soup.find_all("div",class_="related-info")
	for detail in movie_detail:
          #对爬取的电影剧情简介进行字符串的处理 print ("The movie detail is : "+detail.span.text.split('\n')[1].strip()+detail.span.text.split('\n')[2].strip()) number=1 for i in range(4):#爬取top100 ,4个页面。爬取top250,10个页面 url='https://movie.douban.com/top250?start={}'.format(i*25) html=requests.get(url) soup=BeautifulSoup(html.text,'html.parser') #movie_title=[] #movie_link=[] movie_all=soup.find_all("div",class_="hd") for each in movie_all: #print (each.a.span.text) print ("-"*100) print ("The movie is Top : "+str(number)) print ("The movie name is : "+each.a.span.text) print ("The movie link is : "+each.a['href']) get_information(each.a['href']) number+=1

  

  

posted @ 2017-10-07 00:23  wgq0_0  阅读(1871)  评论(0编辑  收藏  举报