分两步:
①爬取网易新闻标题和链接
②存入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 pd
from mysql import connector
import urllib2
import re
from bs4 import BeautifulSoup
import time
url="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