[python爬虫]使用Python爬取网易新闻
来源:互联网 发布:淘宝卖会员账号 编辑:程序博客网 时间:2024/06/05 16:15
分两步:
①爬取网易新闻标题和链接
②存入mysql中
上代码!
# -*- coding: utf-8 -*-"""Created on Thu Apr 06 17:04:37 2017@author: Administrator"""# -*- coding: utf-8 -*-"""Created on Thu Apr 06 15:00:19 2017@author: Administrator"""import pandas as pdfrom mysql import connectorimport urllib2import refrom bs4 import BeautifulSoupimport timeurl="http://www.163.com"html=urllib2.urlopen(url).read()soup=BeautifulSoup(html)links=soup.find_all(name="a",attrs={"href":re.compile("http://news")})z=[]for i in links: z.append((i.get("href"), i.get_text(), time.strftime("%Y-%m-%d %X", time.localtime())))df=pd.DataFrame(z)df.columns=["news_url","news_title","record_time"] #df列名要和mysql中的字段名一致"""**************************分割线以下为mysql操作*********************************************""""""CREATE TABLE 'news2' ('news_url' TEXT,'news_title' VARCHAR(100) DEFAULT NULL,'record_time' DATETIME NOT NULL,'ID' INT(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY ('ID'));ENGINE=InnoDB DEFAULT CHARSET=UTF8"""conn=connector.Connect( host='localhost', port = 3306, user='root', passwd='123', db ='neteasynews',)cur=conn.cursor()df.to_sql("news",conn,flavor="mysql",if_exists="append")cur.close()这几天一直没有找到好的办法使Python自动定时运行,更新后存入数据库。如果有好的办法请留言,谢谢。
0 0
- [python爬虫]使用Python爬取网易新闻
- python 爬取网易新闻评论
- 使用python网络爬虫爬取新浪新闻(一)
- Python爬虫爬取网站新闻
- python爬虫爬取Bloomberg新闻
- python爬虫之爬取腾讯新闻
- (6)Python爬虫——爬取中新网新闻
- python爬虫爬取合肥工业大学校园新闻
- 第四课 Python爬虫简单爬取新浪新闻列表
- 网易新闻评论爬虫(python+selenium+PhantomJS)
- 爬虫第三战 json爬取网易新闻
- Python 3爬虫网易云(八)—— 对网易云歌词的爬取
- Python爬取新浪新闻
- python爬取网易评论
- python爬虫案例系列教程——python爬取百度新闻RSS数据
- 100行python代码爬取5万条网易新闻评论
- python爬虫搜狐新闻
- python爬虫,爬取虎扑网新闻
- C++ STL/ (13) 常用遍历算法
- bzoj 4199: [Noi2015]品酒大会 后缀数组+并查集
- ImportError: cannot import name NUMPY_MKL
- 模块化和组件化的理解
- 【linux】chwon和chmod区别
- [python爬虫]使用Python爬取网易新闻
- Jquery实现无限级树状结构并动态添加增删改等编辑功能
- CodeForces 796A Buying A House
- C++中break、continue、return的区别
- Scala中的下划线到底有多少种应用场景?
- Spring学习-19:Spring的AOP:带有切点的切面
- 02.两个经典的Oracle触发器示例
- 论文笔记:Histopathological Image Classification Using Discriminative Feature-Oriented Dictionary Learnin
- 键盘事件