python爬虫:爬取动态网页并将信息存入MySQL数据库
目标网站
http://www.neeq.com.cn/disclosure/supervise.html
爬取网页该部分内容
网页分析
查看网页源代码发现没有表格部分内容,对网页请求进行分析 F12–>network---->xhr
接下来分析数据来源,切换到Headers可以查看到 url,请求方法为 POST以及 Form Data
Request URL: http://www.neeq.com.cn/disclosureInfoController/infoResult.do?callback=jQuery331_1603957757976Request Method: POSTForm Datapage: 0companyCd: disclosureType: 8startTime: 2019-10-30endTime: 2020-10-29keyword: sortfield: xxssdqsorttype: asc
切换页数多分析几条请求发现 post请求 Form Data 上传了两个关键的数据:disclosureType和page
代码编写
接下来开始撸代码,代码运行环境为 python3 ,结合返回数据格式编写代码
新建 neeq.py 抓取清洗数据
import requests
import json
import py_mysql
import osdef get_data():urls = "http://www.neeq.com.cn/disclosureInfoController/infoResult.do?callback=jQuery331_1603846097415"headers = {'Accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01','Accept-Encoding': 'gzip, deflate','Accept-Language': 'zh-CN,zh;q=0.9','Cache-Control': 'no-cache','Connection': 'keep-alive','Content-Length': '113','Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','Cookie': 'Hm_lvt_b58fe8237d8d72ce286e1dbd2fc8308c=1603788530; AlteonP=Aa0sI4GSf3sCGftZEvcaAw$$; Hm_lpvt_b58fe8237d8d72ce286e1dbd2fc8308c=1603846029','Host': 'www.neeq.com.cn','Origin': 'http://www.neeq.com.cn','Pragma': 'no-cache','Referer': 'http://www.neeq.com.cn/disclosure/supervise.html','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36','X-Requested-With': 'XMLHttpRequest'}# 爬取第四页数据for index in range(4, 5):# post请求获取数据str_data = requests.post(url=urls, headers=headers, data={'page': index, 'disclosureType': 8}).text# 截取字符串有用数据部分并反序列化为字典对象tar_data = json.loads(str_data[str_data.index('[') + 1:str_data.rindex(']')])# 获取需要保存的数据content_data = tar_data['listInfo']for da in content_data['content']:# 保存 pdf,根据返回 pdf 路径下载保存 pdf 到本地urls_pdf = """http://www.neeq.com.cn/""" + da['destFilePath']data_pdf = requests.get(url=urls_pdf).contentpath_pdf = da['destFilePath'][1:]path_dir = path_pdf[: path_pdf.rindex('/') + 1]file_name = path_pdf[path_pdf.rindex('/') + 1:]# 如果pdf文件路径不存在,则新建if not os.path.exists(path_dir):os.makedirs(path_dir)with open(path_dir + file_name, 'wb') as f:f.write(data_pdf)# 将爬取到的数据存入MySQL数据库py_mysql.insert(da['companyCd'], da['companyName'], da['disclosureTitle'], path_pdf,da['publishDate'])# 关闭数据库连接py_mysql.close_()get_data()
新建 py_mysql.py 将数据存入数据库
import pymysqlconn = pymysql.connect(host="127.0.0.1", user="root", password="123456", database="python_test", charset="utf8")
cursor = conn.cursor()def insert(code, alias, title, file, date):sql = """insert into neeq(code,alias,title,file,date_) values("%s","%s","%s","%s",'%s')""" % (code, alias, title, file, date)print(sql)try:cursor.execute(sql)conn.commit()except:print("插入错误")conn.rollback()def close_():cursor.close()conn.close()
查看数据
mysql 保存数据
保存的 pdf
以上就是简单的爬虫入门
python爬虫:爬取动态网页并将信息存入MySQL数据库相关推荐
- chrome动态ip python_用Python爬虫爬取动态网页,附带完整代码,有错误欢迎指出!...
系统环境: 操作系统:Windows8.1专业版 64bit Python:anaconda.Python2.7 Python modules:requests.random.json Backgro ...
- Python爬虫爬取动态网页
系统环境: 操作系统:Windows8.1专业版 64bit Python:anaconda.Python2.7 Python modules:requests.random.json Backgro ...
- mysql好评中评统计_scrapy爬取京东笔记本及评论信息存入MySQL数据库
爬取思路 1.分析页面,定义爬取字段 2.观察网页,分析接口url,通过xpath和json解析爬取内容字段 3.在pipelines.py写入存储方式 4.开始爬取 5.GitHub地址:https ...
- python爬虫爬取当当网的商品信息
python爬虫爬取当当网的商品信息 一.环境搭建 二.简介 三.当当网网页分析 1.分析网页的url规律 2.解析网页html页面 书籍商品html页面解析 其他商品html页面解析 四.代码实现 ...
- python爬虫爬取大众点评店铺简介信息
python爬虫爬取大众点评店铺简介信息 写作目的: 爬取目标 大众点评的保护机制 应对方法 还存在的问题 写作目的: 今天帮朋友一个忙,要爬取一些大众点评上的数据.结果发现大众点评的防爬机制还挺多的 ...
- Scrapy爬取当当网的商品信息存到MySQL数据库
Scrapy爬取当当网的商品信息存到MySQL数据库 Scrapy 是一款十分强大的爬虫框架,能够快速简单地爬取网页,存到你想要的位置.经过两天的摸索,终于搞定了一个小任务,将当当网的商品信息爬下来存 ...
- Scrapy爬取重庆安居客二手房并存入mysql数据库(下)
上篇中我们获取了重庆的一二级区(Scrapy爬取重庆安居客二手房并存入mysql数据库(上)),这一篇我们根据二级区获取相应的二手房信息. 初始化数据库 创建二手房信息数据库表,house表存放二手房 ...
- python网页数据存入数据库_python网络爬虫抓取动态网页并将数据存入数据库MySQL...
简述 以下的代码是使用python实现的网络爬虫,抓取动态网页 http://hb.qq.com/baoliao/ .此网页中的最新.精华下面的内容是由JavaScript动态生成的.审查网页元素与网 ...
- python网站数据写入mysql_python网络爬虫抓取动态网页并将数据存入数据库MySQL
简述 以下的代码是使用python实现的网络爬虫,抓取动态网页 http://hb.qq.com/baoliao/ .此网页中的最新.精华下面的内容是由JavaScript动态生成的.审查网页元素与网 ...
最新文章
- 倒排索引优化 - 跳表求交集 空间换时间 贪心
- 蓝牙BLE4.0的LL层数据和L2CAP层数据的区分与理解
- zoj 3204 Connect them kruskal
- loadView与viewDidLoad不同
- 运行Python时出现SyntaxError: EOL while scanning string literal解决方法
- 一万块是存入支付宝里的余额宝好还是存在微信的零钱通里好?
- 软件设计师备考知识04
- getHandel redis_Controller层利用Redis实现分布式锁(注解实现)
- Minix、UNIX、Linux的区别与联系
- 现代数字图像处理---lena图像处理
- 我国4G标准拓展国际市场取得重要突破
- Qt 3D 学习路径规划
- deeplearning.36车辆识别YOLO算法实践
- excel小技巧之如何提取指定字符之前的字段
- 漂亮的CSS背景颜色
- npm ERR! File exists: /XXX/xxx npm ERR! Move it away, and try again.
- BTSP——HDU2224The shortest path
- m3u8.sqlite转mp4(txkt,文末附程序下载地址)
- 辰星人才 | 旷视实习生鲁盼:UCLA朱松纯准博士,两年七篇顶会论文
- 经济与金融大数据挖掘——知识点总结回顾