#利用requests和正则抓取猫眼电影网上排名前100的电影
import requests
from requests.exceptions import RequestException
import re
import json
from multiprocessing import Pool def get_one_page(url):
try:
#需要重置requests的headers,否则猫眼电影拒绝访问。
headers = {
"user-agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
response = requests.get(url,headers=headers)
#加一个判断,判断请求URL是否成功
if response.status_code == 200:
return response.text
return None
except RequestException:
return None def parse_one_page(html):
#解析网页的html代码
pattern = re.compile('<dd>.*?board-index-1.*?>(\d+)</i>.*?src="(.*?)".*?name"><a'
'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>'
'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S) items = re.findall(pattern,html)
#print(items)
for item in items:
yield {
'index':item[0],
'image':item[1],
'title':item[2],
'actor':item[3].strip()[3:],
'time':item[4].strip()[5:],
'score':item[5]+item[6]
} def write_to_file(content):
with open('result.txt','a') as f:
f.write(json.dumps(content)+'\n')
f.close() def main(offset):
url = 'http://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)
# print(html) if __name__ == '__main__':
#设置多进程抓取
pool = Pool()
pool.map(main,[i*10 for i in range(10)])

  

Python爬虫【三】利用requests和正则抓取猫眼电影网上排名前100的电影的更多相关文章

  1. Python爬虫工程师必学——App数据抓取实战 ✌✌

    Python爬虫工程师必学——App数据抓取实战 (一个人学习或许会很枯燥,但是寻找更多志同道合的朋友一起,学习将会变得更加有意义✌✌) 爬虫分为几大方向,WEB网页数据抓取.APP数据抓取.软件系统 ...

  2. Python爬虫工程师必学APP数据抓取实战✍✍✍

    Python爬虫工程师必学APP数据抓取实战  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大 ...

  3. Python爬虫工程师必学——App数据抓取实战

    Python爬虫工程师必学 App数据抓取实战 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大 ...

  4. python+requests+re匹配抓取猫眼上映电影信息

    python+requests抓取猫眼中上映电影,re正则匹配获取对应电影的排名,图片地址,片名,主演及上映时间和评分 import requests import re, json def get_ ...

  5. [Python爬虫] 之十三:Selenium +phantomjs抓取活动树会议活动数据

    抓取活动树网站中会议活动数据(http://www.huodongshu.com/html/index.html) 具体的思路是[Python爬虫] 之十一中抓取活动行网站的类似,都是用多线程来抓取, ...

  6. [Python爬虫] 之十一:Selenium +phantomjs抓取活动行中会议活动信息

    一.介绍 本例子用Selenium +phantomjs爬取活动行(http://www.huodongxing.com/search?qs=数字&city=全国&pi=1)的资讯信息 ...

  7. [Python爬虫] 之十:Selenium +phantomjs抓取活动行中会议活动

    一.介绍 本例子用Selenium +phantomjs爬取活动树(http://www.huodongshu.com/html/find_search.html?search_keyword=数字) ...

  8. Python爬虫系列-分析Ajax请求并抓取今日头条街拍图片

    1.抓取索引页内容 利用requests请求目标站点,得到索引网页HTML代码,返回结果. 2.抓取详情页内容 解析返回结果,得到详情页的链接,并进一步抓取详情页的信息. 3.下载图片与保存数据库 将 ...

  9. Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分

    1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...

随机推荐

  1. 实习培训——Java多线程(9)

    实习培训——Java多线程(9) 很适合新手 http://www.cnblogs.com/GarfieldEr007/p/5746362.html http://www.cnblogs.com/Ga ...

  2. Marathon自动扩缩容(marathon-lb-autoscale)

    我们在服务里面创建如下的应用(以下是创建完复制过来的json): { "id": "/nginxtest", "cmd": null, &q ...

  3. BiLSTM学习

    转自:https://blog.csdn.net/aliceyangxi1987/article/details/77094970 https://blog.csdn.net/jojozhangju/ ...

  4. mac xcode 常见配置

    1.报错:There are no schemes in workspace "..." 设置scheme共享,方法: 2.Build 文件夹是中间文件的保存地方,如何设置在工程目 ...

  5. [LeetCode] 100. Same Tree_Easy tag: DFS

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  6. [LeetCode] 278. First Bad Version_Easy tag: Binary Search

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  7. linux shell脚本检测硬盘磁盘空间 邮件报警

    使用 http://www.weiruoyu.cn/?p=368 shell脚本监控硬盘空间剩余空间 邮件报警 1.先观察一下磁盘,和如何使用脚本 [root@localhost ~]# df -h ...

  8. python快速开发Web之Django

    以前写测试框架,要么是纯python代码驱动的,要么是WinForm界面的框架,多人操作并不适合. 就想用python写个Web版的,于是想到了Web快速开发的框架Flask和Django两个 个人觉 ...

  9. Visual Studio 2015 开发Android Cordova出现unsupported major minor version 52.0错误的解决方法

    JDK版本的问题,需要JDK1.8版本,安装!VS2015做如下设置, 工具->选项->用于Apache Cordoba的工具->环境变量替代->JAVA_HOME设为1.8:

  10. 日期条控件 DateFieldControl

    日期条控件 书:163 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns ...