import json
# from multiprocessing import Pool
import requests
from requests.exceptions import RequestException
import re def get_one_page(url):
try:
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)
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()[3:],
'time': item[4].strip()[5:],
'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 = 'http://maoyan.com/board/4?offset='+str(offset)
# url='http://www.baidu.com'
html = get_one_page(url)
# print(html)
for item in parse_one_page(html):
print(item)
write_to_file(item) if __name__ == '__main__':
for i in range(10):
main(i*10)

注:需要重置requests的headers,否则猫眼电影拒绝访问。

利用正则+requests爬取猫眼电影信息的更多相关文章

  1. Python3爬取猫眼电影信息

    Python3爬取猫眼电影信息 import json import requests from requests.exceptions import RequestException import ...

  2. 爬虫基本库request使用—爬取猫眼电影信息

    使用request库和正则表达式爬取猫眼电影信息. 1.爬取目标 猫眼电影TOP100的电影名称,时间,评分,等信息,将结果以文件存储. 2.准备工作 安装request库. 3.代码实现 impor ...

  3. 爬虫--requests爬取猫眼电影排行榜

    '''目标:使用requests分页爬取猫眼电影中榜单栏目中TOP100榜的所有电影信息,并将信息写入文件URL地址:http://maoyan.com/board/4 其中参数offset表示其实条 ...

  4. 使用requests爬取猫眼电影TOP100榜单

    Requests是一个很方便的python网络编程库,用官方的话是"非转基因,可以安全食用".里面封装了很多的方法,避免了urllib/urllib2的繁琐. 这一节使用reque ...

  5. PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)

    利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...

  6. 用requests库爬取猫眼电影Top100

    这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...

  7. python爬取猫眼电影top100

    最近想研究下python爬虫,于是就找了些练习项目试试手,熟悉一下,猫眼电影可能就是那种最简单的了. 1 看下猫眼电影的top100页面 分了10页,url为:https://maoyan.com/b ...

  8. 14-Requests+正则表达式爬取猫眼电影

    '''Requests+正则表达式爬取猫眼电影TOP100''''''流程框架:抓去单页内容:利用requests请求目标站点,得到单个网页HTML代码,返回结果.正则表达式分析:根据HTML代码分析 ...

  9. 50 行代码教你爬取猫眼电影 TOP100 榜所有信息

    对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天,恋习Python的手把手系列,手把手教你入门Python爬虫,爬取猫眼电影TOP100榜信息,将涉及到基础爬虫 ...

随机推荐

  1. 爬取qq号

    import reimport urllib.requestimport osimport jsonimport sslfrom collections import deque #把爬去的数据保存到 ...

  2. appium定位toast消息的使用

    定位使用xpath后,定位消息文本,然后使用text获取消息文本做断言.toast_loc = ("xpath", ".//*[contains(@text,'切换运营商 ...

  3. python写注册

    # coding = UTF-8 注释格式 import datetime 引用日期 today = datetime.datetime.today().strftime("%Y-%m-%d ...

  4. JSP 页面跳转的实现方法

    客户端跳转 1. 使用 href 超链接标记  <a href="new.jsp">跳转</a> 2. 使用表单提交完成跳转  <form actio ...

  5. Python之路(第三十八篇) 并发编程:进程同步锁/互斥锁、信号量、事件、队列、生产者消费者模型

    一.进程锁(同步锁/互斥锁) 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的, 而共享带来的是竞争,竞争带来的结果就是错乱,如何控制,就是加锁处理. 例 ...

  6. windows2012 raid架构 忘记系统管理员密码的解决方法

    1. http://bbs.51cto.com/thread-939710-1.html 2. https://wenku.baidu.com/view/115783cd0b4e767f5acfcef ...

  7. Python3 在操作excel时报PermissionError: [Errno 13] Permission denied: 'D:/workstation/yhdx_auto/config/case.xls'错误

    此问题为文件被拒绝访问,主要是该文件已经被打开了.关闭此excel文件后在执行excel的相关操作就ok了.

  8. python networkx:绘制网络图

    1.简单使用 import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() G.add_edge(1,2) nx.draw_ ...

  9. amazeui分页

    <link rel="stylesheet" href="../../static/css/manage/amazeui.min.css" /> & ...

  10. [C#.Net]Window服务调用外部程序

    最近遇到要做回传服务内增加开关,可以自定义运行一些脚本已方便收集PC状态,发现Bat始终无法运行,上网找了半天才发现和Session0有关,也就是程序有不同级别的访问权限,Vista以上版本为了安全因 ...