python爬虫猫眼电影排行top100实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python爬虫猫眼电影排行top100实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1356字,纯文字阅读大概需要2分钟。
内容图文
![python爬虫猫眼电影排行top100实例](/upload/InfoBanner/zyjiaocheng/837/6ea2ce27d53544e0b2af63b8f672baff.jpg)
今天是个好天气,培训了一个月了,可以看的懂python代码,一直对爬虫比较感兴趣,今天星期六没上课就看视频,跟着老师敲代码,中间各种错误,到饭点了才弄好,成功爬取!这个时刻也是值得纪念的,心情和天气一样晴朗。感兴趣的朋友也可以照下面的代码自己敲一遍,运行一下看看效果。
import requests,re
from requests.exceptions import RequestException
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(’
+’.?>(.?).?star">(.?)
+’.?integer">(.?).?fraction">(.?).?’,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 = “https://maoyan.com/board/4?offset=”+str(offset)
html = get_one_page(url)
parse_one_page(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)
告诉你使我达到目标的奥秘吧,我唯一的力量就是我的坚持精神。
内容总结
以上是互联网集市为您收集整理的python爬虫猫眼电影排行top100实例全部内容,希望文章能够帮你解决python爬虫猫眼电影排行top100实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。