环境
1. win10
2. python3
分析源码
打开猫眼Top100榜,每一栏电影都有一定规律,以霸王别姬为例分析
<dd>
<i class="board-index board-index-1">1</i>
<a href="/films/1203" title="霸王别姬" class="image-link" data-act="boarditem-click" data-val="{movieId:1203}"> <img src="//s0.meituan.net/bs/?f=myfe/mywww:/image/loading_2.e3d934bf.png" alt="" class="poster-default" /> <img alt="霸王别姬" class="board-img" src="https://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c" /> </a>
<div class="board-item-main">
<div class="board-item-content">
<div class="movie-item-info">
<p class="name"><a href="/films/1203" title="霸王别姬" data-act="boarditem-click" data-val="{movieId:1203}">霸王别姬</a></p>
<p class="star"> 主演:张国荣,张丰毅,巩俐 </p>
<p class="releasetime">上映时间:1993-01-01</p>
</div>
<div class="movie-item-number score-num">
<p class="score"><i class="integer">9.</i><i class="fraction">5</i></p>
</div>
</div>
</div>
</dd>
需要抓取
- 排名(index)
- 图片(image)
- 电影名字(title)
- 主演(actor)
- 上映时间(time)
- 分数(score)
实战多线程抓取
1.创建请求
def get_one_page(url):
try:
response = requests.get(url);
if response.status_code == 200:
return response.text
return None
except RequestException:
return None
2.正则匹配并数据清洗
def parse_one_page(html):
pattern = re.compile('<dd>.*?board-index.*?(\d+)</i>.*?data-src="(.*?)".*?name"><a'
+'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?'
+'integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
items = re.findall(pattern,html)
for item in items:
yield {
'index':item[0],
'image':item[1],
'title':item[2],
'actor':item[3].strip(),
'time':item[4],
'score':item[5]+item[6],
}
3.写入文件
def write_to_file(content):
with open('result.txt','a',encoding='utf-8') as f:
f.write(json.dumps(content,ensure_ascii=False) + '\n')
f.close()
4.开始抓取
def main(offset):
url = 'https://maoyan.com/board/4?offset='+str(offset)
html = get_one_page(url)
for item in parse_one_page(html):
print(item)
write_to_file(item)
if __name__ == '__main__':
# for i in range(10):
# main(i*10)
#进程池抓取
pool = Pool()
pool.map(main,[i*10 for i in range(10)])
完整代码如下:
# Author:Alex Jakin
from multiprocessing import Pool
import requests
from requests.exceptions import RequestException
import re
import json
def get_one_page(url):
try:
response = requests.get(url);
if response.status_code == 200:
return response.text
return None
except RequestException:
return None
def parse_one_page(html):
pattern = re.compile('<dd>.*?board-index.*?(\d+)</i>.*?data-src="(.*?)".*?name"><a'
+'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?'
+'integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
items = re.findall(pattern,html)
for item in items:
yield {
'index':item[0],
'image':item[1],
'title':item[2],
'actor':item[3].strip(),
'time':item[4],
'score':item[5]+item[6],
}
def write_to_file(content):
with open('result.txt','a',encoding='utf-8') as f:
f.write(json.dumps(content,ensure_ascii=False) + '\n')
f.close()
def main(offset):
url = 'https://maoyan.com/board/4?offset='+str(offset)
html = get_one_page(url)
for item in parse_one_page(html):
print(item)
write_to_file(item)
if __name__ == '__main__':
# for i in range(10):
# main(i*10)
#进程池抓取
pool = Pool()
pool.map(main,[i*10 for i in range(10)])