Python 爬虫之第一次接触
2024-08-31 04:25:14
爬豆瓣网电影TOP250名单 ------- 代码未写完,等待更新
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("", re.S)#笔记本屏幕太小看不过来
items = re.findall(pattern, html) for item in items:#把爬取的信息进行格式化
yield{
"":item[0],
"":item[1],
} 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():
url = "https://movie.douban.com/top250"
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__":
main() #这段代码没有加RE的匹配方式(笔记本屏幕小)
#第二个这段代码只能匹配一页的东西,自己发挥想象把
最新文章
- (转)SVN服务器搭建和使用(一)
- High购电商系统开发注意点
- 在Fedora 20 上安装Mysql并初始化root密码
- iOS开发技巧
- 用Javascript取float型小数点
- tcpdump抓包
- Centos 安装 p7zip,即Linux下的7z
- [设计模式]Netd中的命令设计模式
- php使用mysql_query查询超大结果集超内存的解决方法
- 扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注
- Android开发之SoundPool使用具体解释
- Vue-cli 记录
- LeetCode 15. 3Sum(三数之和)
- Tomcat内核之ASCII解码的表驱动模式
- SQL优化 MySQL版 -分析explain SQL执行计划与Extra
- AOP - 2 实例(SpringBoot 注解方式)
- 算法实践--最长公共子序列(Longest Common Subsquence)
- free命令详解
- Python把两个列表合成一个字典
- Getting started with Processing 第十三章——延伸(1)
热门文章
- 【bzoj1028】[JSOI2007]麻将
- bzoj3332: 旧试题
- hdu 4123(树形dp+倍增)
- 安装nghttp2 报错error: Libtool library used but 'LIBTOOL' is undefined
- Timer A UP mode 中断
- 一款使用C# .NET开发的SIP网络电话客户端完整源码
- PCB Genesis脚本 C#调用Javascript
- 基于ASP.Net Core开发一套通用后台框架记录-(项目的搭建)
- Linux 本命令 基本上用到的命令-自己留着用
- 动态规划DP入门