# coding: utf-8
# In[4]:
#導入包
import requests
from bs4 import BeautifulSoup
#爬取特定網頁
res = requests.get("https://news.sina.com.cn/china/")
#轉化文字編碼
res.encoding = 'utf-8'
#存進BeautifulSoup元素中
soup = BeautifulSoup(res.text, 'html.parser')
#print(soup)
for news in soup.select('.news-1'):#爬取並遍歷所有class爲"news_1”的元素
li = news.select('li')#選取所有含有'li'特定標籤的元素,並存進li這個list中去
num = len(li)#獲取到元素的個數
if num > 0:
for i in range(0, num):
print(li[i].text)
#a = '<a href = "#" abc = 456 def = 123> i am a link </a>'
#soup = BeautifulSoup(a, 'html.parser')
#print(soup.select('a')[0]['href'])#輸出"#"
#print(soup.select('a')[0]['abc'])#輸出"456"
#print(soup.select('a')[0]['def'])#輸出"123"
#soup.select('#main-title')[0].text
# In[5]:
soup.select("#_blank")[0].text
# In[6]:
#導入包
import requests
from bs4 import BeautifulSoup
#爬取特定網頁
res = requests.get("http://news.sina.com.cn./c/nd/2016-08-20/doc-ifxvctcc8121090.shtml")
#轉化文字編碼
res.encoding = 'utf-8'
#存進BeautifulSoup元素中
soup = BeautifulSoup(res.text, 'html.parser')
#print(soup)
#for big in soup.select('.right_content"):
#for news_1 in soup.select('.news-1'):#爬取並遍歷所有class爲"news_1”的元素,抓取類的時候,要加上'.'
# li = news_1.select('li')#選取所有含有'li'特定標籤的元素,並存進li這個list中去
# a = news_1.select('a')#選取所有含有'a'標籤的元素
# num = len(li)#獲取到元素的個數
#if num > 0:
# for i in range(0, num):
# print(li[i].text,a[i]['href'])
#print(soup)
#for news_2 in soup.select(".news-2"):
# li
#a = '<a href = "#" abc = 456 def = 123> i am a link </a>'
#soup = BeautifulSoup(a, 'html.parser')
#print(soup.select('a')[0]['href'])#輸出"#"
#print(soup.select('a')[0]['abc'])#輸出"456"
#print(soup.select('a')[0]['def'])#輸出"123"
# In[5]:
soup.select('#artibodyTitle')[0].text #選取id爲artibodyTitle的標籤,前面要加'#' 輸出文字內容
# In[5]:
soup.select('.time-source')[0] #選取class爲time-source的標籤,也可以用id來選取:select('#navtimeSource')
# In[6]:
soup.select('.time-source')[0].text #選取class爲time-source的標籤,也可以用id來選取:select('#navtimeSource') 輸出文字內容
# In[14]:
soup.select('.time-source')[0].contents #選取class爲time-source的標籤,也可以用id來選取:select('#navtimeSource') 選取所包含的所有標籤
# In[15]:
soup.select('.time-source')[0].contents[0] #選取class爲time-source的標籤,也可以用id來選取:select('#navtimeSource') 選取所包含內容的第一個標籤
# In[21]:
timesource = soup.select('.time-source')[0].contents[0].strip() #選取class爲time-source的標籤,也可以用id來選取:select('#navtimeSource') 選取所包含內容的第一個標籤,並忽略多餘的換行
type(timesource) #此時得到的時間是字符類型
# In[27]:
from datetime import datetime #導入包
dt = datetime.strptime(timesource,'%Y年%m月%d日%H:%M') #將字符類型轉爲時間類型
print(dt)
# In[29]:
dt.strftime('%d-%Y-%M') #將時間類型的dt轉爲任意自定義格式
# In[30]:
soup.select('.time-source')[0] #選取class爲time-source的標籤,也可以用id來選取:select('#navtimeSource')
# In[33]:
soup.select('.time-source span')[0] #選取class爲time-source的標籤span下的span標籤
# In[34]:
soup.select('.time-source span a')[0] #選取class爲time-source的標籤span下的span下的a標籤
# In[35]:
soup.select('.time-source span a')[0].text #選取class爲time-source的標籤span下的span下的a標籤
# In[13]:
soup.select('#artibody') #選取id爲artibody的標籤
#soup.select('.article article_16') #選取id爲artibody的標籤 這種方法不行。。我也不知道爲什麼。。。
# In[14]:
soup.select('#artibody p') #選取id爲artibody的標籤中的p標籤
# In[16]:
soup.select('#artibody p')[:-1] #選取id爲artibody的標籤的p標籤 並去除最後一個p標籤
# In[17]:
article = [] #用於存放文章內容
for p in soup.select('#artibody p')[:-1]:
article.append(p.text) #將每一個p標籤內的內容存入article中去
print(article)
# In[18]:
article = [] #這個列表用於存放各個p標籤內的文字
for p in soup.select('#artibody p')[:-1]:
article.append(p.text.strip()) #將每一個p標籤內的文字(去除換行等字符)存入article中去
print(article)
# In[22]:
article = [] #這個列表用於存放各個p標籤內的文字
for p in soup.select('#artibody p')[:-1]:
article.append(p.text.strip()) #將每一個p標籤內的文字(去除換行等字符)存入article中去
print(article)
#' '.join(article) #將列表中的各個元素合併,以' '作爲分隔
'@@@@@'.join(article) #將列表中的各個元素合併,以'@'作爲分隔
# In[23]:
#以上塊可以用一行代碼實現
'@@@@'.join([p.text.strip() for p in soup.select('#artibody p')[:-1]])
# In[24]:
soup.select('.article-editor') #選取class爲article-editor的標籤
# In[26]:
soup.select('.article-editor')[0].text #選取class爲article-editor的標籤,並輸出標籤文字
# In[27]:
soup.select('.article-editor')[0].text.strip("責任編輯:") #選取class爲article-editor的標籤,並輸出標籤文字, 並移除”責任編輯:“文字
# In[8]:
soup.select('#commentCount1')
# In[19]:
comment = requests.get('http://comment5.news.sina.com.cn/page/info?version=1&format=js&channel=gn&newsid=comos-fxvctcc8121090&group=&compress=0&ie=utf-8&oe=utf-8&page=1&page_size=20') #這個網址是包含評論的網址 得在network下尋找對應的相應包
import json
jd = json.loads(comment.text.strip('var data=')) #通過json模塊下的函數 將網頁讀進成一個字典, 並用strip()去除js的格式“var data="
# In[21]:
jd['result']['count']['total'] #取出字典中的total值
# In[22]:
newsrul = 'http://news.sina.com.cn./c/nd/2016-08-20/doc-ifxvctcc8121090.shtml'
newsrul.split('/') #根據'/'進行切割,切割好的內容放入字典中
# In[23]:
newsrul.split('/')[-1] #根據'/'進行切割,切割好的內容放入字典中, 並取出字典中的最後一個元素
# In[24]:
newsrul.split('/')[-1].rstrip('shtml') #根據'/'進行切割,切割好的內容放入字典中, 並取出字典中的最後一個元素, 去除'shtml',其中rstrip中的r表示從右開始移除
# In[28]:
newsid = newsrul.split('/')[-1].rstrip('.shtml').lstrip('doc-') #......去除'shtml',其中rstrip中的r表示從右開始移除,從左移除’doc-'
newsid
# In[37]:
#以上取得newsid的操作也可以通過正則表達式實現
import re
m = re.search('doc-i(.+).shtml',newsrul) #第一個參數是要尋找的內容的格式 第二個參數是所尋找的目的源
m
# In[38]:
m.group(0)
# In[41]:
newsid = m.group(1)
newsid