1.內容爬取
首先上代碼:
import requests
from bs4 import BeautifulSoup
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
for i in range(1,8):
link='https://blog.csdn.net/lws123253/article/list/'+str(i)+'?'
r=requests.get(link,headers=headers)
soup=BeautifulSoup(r.text,"html.parser")
title_list=soup.find_all('div',class_="article-item-box csdn-tracking-statistics")
for title in title_list:
print(title.a.contents[2].strip())#注意這裏的寫法
代碼中其它地方沒什麼好說的,這裏主要講一下最後一行代碼:
print(title.a.contents[2].strip())
我們先觀察下我們需要爬取的內容的網頁代碼:
我可以發現,我們需要的信息被包含在"a"標籤中, 但是由於我們沒有辦法通過“a”標籤直接定位我們的需要信息,所以我們找它的上級,可以通過一些所有標題通用“class”等來標識,然後一步步往下走。比如我們可以通過“div”->"a"->"信息",但是注意a標籤裏還包含文章標題前面的“原創”或者“轉載”標記,即“原”或者“轉”。所以這時候我們得另想其它辦法,我們可以通過a.contnets[2].strip()獲取。如果這裏不清楚contens的下標應該是多少,可以先打印a.contents,然後找到我們需要的內容的位置,就可以判斷下標了。
2.存儲
2.1 存儲至txt文件
上面的代碼,稍微調整後:
import requests
from bs4 import BeautifulSoup
with open(r'C:\Users\AdenLee\Desktop\blog.txt','a+') as f:
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
for i in range(1,8):
link='https://blog.csdn.net/lws123253/article/list/'+str(i)+'?'
r=requests.get(link,headers=headers)
soup=BeautifulSoup(r.text,"html.parser")
title_list=soup.find_all('div',class_="article-item-box csdn-tracking-statistics")
for title in title_list:
#print(title.a.contents[2].strip())#直接輸出,注意這裏的寫法
f.write(title.a.contents[2].strip()+'\n')#存入文件
f.close()
2.2 存儲至csv文件
import requests
from bs4 import BeautifulSoup
import csv
with open(r'C:\Users\AdenLee\Desktop\blog.csv','a+',encoding='UTF-8',newline='') as f:#newline=''的作用是每一行之間沒有間隙,否則會有空行
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
for i in range(1,8):
link='https://blog.csdn.net/lws123253/article/list/'+str(i)+'?'
r=requests.get(link,headers=headers)
soup=BeautifulSoup(r.text,"html.parser")
title_list=soup.find_all('div',class_="article-item-box csdn-tracking-statistics")
for title in title_list:
#print(title.a.contents[2].strip())#直接輸出,注意這裏的寫法
#f.write(title.a.contents[2].strip()+'\n')#存入文件
w=csv.writer(f)
#w.writerow(title.a.contents[2].strip())
#注意下面的寫法,如果不把保存的內容加上[],由於writerow將一個列表寫入文件,則一個字符串中每一個字符則會被當做一個元素
w.writerow([title.a.contents[2].strip()])
f.close()
這裏強調一下寫入的時候的寫法: w.writerow([title.a.contents[2].strip()]),注意觀察裏面是有一箇中括號的。
3.讀取
3.1 從txt文件讀取
import requests
from bs4 import BeautifulSoup
with open(r'C:\Users\AdenLee\Desktop\blog.txt','r') as f:
result=f.read()
print(result)
f.close()
3.2從csv文件讀取
import requests
from bs4 import BeautifulSoup
import csv
#注意下面要添加:encoding='UTF-8',否則會報如下錯誤
# 'gbk' codec can't decode byte 0xac in position 14: illegal multibyte sequence
with open(r'C:\Users\AdenLee\Desktop\blog.csv','r',encoding='UTF-8') as f:
csv_reader=csv.reader(f)
for row in csv_reader:
print(row)
f.close()
注意讀取時要添加:encoding='UTF-8',否則會報如下錯誤
'gbk' codec can't decode byte 0xac in position 14: illegal multibyte sequence