大家好,我是潤森
網絡爬蟲(又被稱為網頁蜘蛛,網絡機器人,在FOAF社區中間,更經常的稱為網頁追逐者),是一種按照一定的規則,自動地抓取萬維網信息的程序或者腳本。另外一些不常使用的名字還有螞蟻、自動索引、模擬程序或者蠕蟲。(來源: 百度百科)
Robots協議(也稱為爬蟲協議、機器人協議等)的全稱是“網絡爬蟲排除標準”(Robots Exclusion Protocol),網站通過Robots協議告訴搜索引擎哪些頁面可以抓取,哪些頁面不能抓取。
robots.txt文件是一個文本文件,使用任何一個常見的文本編輯器,比如Windows系統自帶的Notepad,就可以創建和編輯它。robots.txt是一個協議,而不是一個命令。robots.txt是搜索引擎中訪問網站的時候要查看的第一個文件。robots.txt文件告訴蜘蛛程序在服務器上什么文件是可以被查看的。(來源: 百度百科)
目標:爬取百度的圖片,并保存電腦中
首先數據是否公開?能不能下載?
由于工作需要,給公司前端做了一個小工具,使用python語言,爬取搜狗微信的微信文章,附搜狗微信官方網址
私信小編01即可獲取Python學習資料
搜狗微信:https://weixin.sogou.com/
從熱門到時尚圈,并且包括每個欄目下面的額加載更多內容選項
?
一共加起來500+篇文章
爬取這些文章獲取到每篇文章的標題和右側的圖片,將爬取到的圖片以規定的命名方式輸出到規定文件夾中,并將文章標題和圖片名稱對應輸出到Excel和txt中
?
?
?
?
Package Version
------------------------- ---------
altgraph 0.17
certifi 2020.6.20
chardet 3.0.4
future 0.18.2
idna 2.10
lxml 4.5.2
pefile 2019.4.18
pip 19.0.3
pyinstaller 4.0
pyinstaller-hooks-contrib 2020.8
pywin32-ctypes 0.2.0
requests 2.24.0
setuptools 40.8.0
urllib3 1.25.10
XlsxWriter 1.3.3
xlwt 1.3.0
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import requests
import xlsxwriter
from lxml import etree
# 請求微信文章的頭部信息
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Host': 'weixin.sogou.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
# 下載圖片的頭部信息
headers_images = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Host': 'img01.sogoucdn.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
a = 0
all = []
# 創建根目錄
save_path = './微信文章'
folder = os.path.exists(save_path)
if not folder:
os.makedirs(save_path)
# 創建圖片文件夾
images_path = '%s/圖片' % save_path
folder = os.path.exists(images_path)
if not folder:
os.makedirs(images_path)
for i in range(1, 9):
for j in range(1, 5):
url = "https://weixin.sogou.com/pcindex/pc/pc_%d/%d.html" % (i, j)
# 請求搜狗文章的url地址
response = requests.get(url=url, headers=headers).text.encode('iso-8859-1').decode('utf-8')
# 構造了一個XPath解析對象并對HTML文本進行自動修正
html = etree.HTML(response)
# XPath使用路徑表達式來選取用戶名
xpath = html.xpath('/html/body/li')
for content in xpath:
# 計數
a = a + 1
# 文章標題
title = content.xpath('./div[@class="txt-box"]/h3//text()')[0]
article = {}
article['title'] = title
article['id'] = '%d.jpg' % a
all.append(article)
# 圖片路徑
path = 'http:' + content.xpath('./div[@class="img-box"]//img/@src')[0]
# 下載文章圖片
images = requests.get(url=path, headers=headers_images).content
try:
with open('%s/%d.jpg' % (images_path, a), "wb") as f:
print('正在下載第%d篇文章圖片' % a)
f.write(images)
except Exception as e:
print('下載文章圖片失敗%s' % e)
# 信息存儲在excel中
# 創建一個workbookx
workbook = xlsxwriter.Workbook('%s/Excel格式.xlsx' % save_path)
# 創建一個worksheet
worksheet = workbook.add_worksheet()
print('正在生成Excel...')
try:
for i in range(0, len(all) + 1):
# 第一行用于寫入表頭
if i == 0:
worksheet.write(i, 0, 'title')
worksheet.write(i, 1, 'id')
continue
worksheet.write(i, 0, all[i - 1]['title'])
worksheet.write(i, 1, all[i - 1]['id'])
workbook.close()
except Exception as e:
print('生成Excel失敗%s' % e)
print("生成Excel成功")
print('正在生成txt...')
try:
with open('%s/數組格式.txt' % save_path, "w") as f:
f.write(str(all))
except Exception as e:
print('生成txt失敗%s' % e)
print('生成txt成功')
print('共爬取%d篇文章' % a)
最后將程序打包成exe文件,在windows系統下可以直接運行程序
?
點贊收藏關注,你的支持是我最大的動力!