#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import requests
from bs4 import BeautifulSoup
start = time.clock()
url = 'http://news.sina.com.cn/china/'
res = requests.get(url)
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
tm = news.select('.time')[0].text
href = news.select('a')[0]['href']
print(h2, tm, href)
usetime = time.clock()-start
print('用时{}秒'.format(usetime))
Scrapy by requests & bs4
最新推荐文章于 2024-08-21 11:27:15 发布