使用python写基本爬虫
以http://news.sina.com.cn/china/
import requests
resp=requests.get('http://news.sina.com.cn/china/')
resp.encoding='uft-8'
from bs4 import BeautifulSoup
html_sample=resp.text
soup=BeautifulSoup(html_sample,'html.parser')
titles=soup.select(''title)
print(titles)
for meta in metas:
print(meta)
IDs=soup.select('#id')
print(IDs)
CLASSs=soup.select('.class')
print(CLASSs)
----------------------------------------------------------------------------------------------
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> from bs4 import BeautifulSoup
>>> res=requests.get('http://news.sina.com.cn/china')
>>> res.encoding='utf-8'
>>> soup=BeautifulSoup(res.text,'html.parser')
>>> for news in soup.select('.news-item'):
... if len(news.select('h2'))>0:
... h2=news.select('h2')[0].text
... time=news.select('.time')[0].text
... a=news.select('a')[0]['href']
... print(time,h2,a)
...
>>>
>>>
时间字符串转换
from datetime import datetime
字符串时间---strptime
dt=datetime.strptime(timesource,'%Y年%m月%d日%H:%M')
时间转字符串:strftime
dt.strftime('%Y-%m-%d')