代码拉取完成,页面将自动刷新
# Author: zqp
# -*- coding: utf-8 -*-
# @Time :2018/1/14 19:17
# @descript:
import json
from multiprocessing import pool
import re
from multiprocessing.pool import Pool
from requests.exceptions import RequestException
import requests
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 paser_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)
itmes = re.findall(pattern, html)
for it in itmes:
yield {
'index': it[0],
'image': it[1],
'title': it[2],
'actor': it[3].strip()[3:],
'time': it[4].strip()[5:],
'score': it[5]+it[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="http://maoyan.com/board/4?offset="+str(offset)
html = get_one_page(url)
for it in paser_one_page(html):
print(it)
write_to_file(it)
if __name__ == '__main__':
# for i in range(10):
# main(10*i)
pool = Pool()
pool.map(main,[i*10 for i in range(10)])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。