前言:
??????個(gè)人簡介:以山河作禮。
??????:Python領(lǐng)域新星創(chuàng)作者,CSDN實(shí)力新星認(rèn)證
?????第一篇文章《1.認(rèn)識(shí)網(wǎng)絡(luò)爬蟲》獲得全站熱榜第一,python領(lǐng)域熱榜第一
。
?? ??第四篇文章《4.網(wǎng)絡(luò)爬蟲—Post請(qǐng)求(實(shí)戰(zhàn)演示)》全站熱榜第八
。
?? ??第八篇文章《8.網(wǎng)絡(luò)爬蟲—正則表達(dá)式RE實(shí)戰(zhàn)》全站熱榜第十二
。
?? ??第十篇文章《10.網(wǎng)絡(luò)爬蟲—MongoDB詳講與實(shí)戰(zhàn)》全站熱榜第八,領(lǐng)域熱榜第二
?? ??第十三篇文章《13.網(wǎng)絡(luò)爬蟲—多進(jìn)程詳講(實(shí)戰(zhàn)演示)》全站熱榜第十二
。
?? ??第十四篇文章《14.網(wǎng)絡(luò)爬蟲—selenium詳講》測試領(lǐng)域熱榜第二十
。
?? ??第十六篇文章《網(wǎng)絡(luò)爬蟲—字體反爬(實(shí)戰(zhàn)演示)》全站熱榜第二十五
。
????《Python網(wǎng)絡(luò)爬蟲》專欄累計(jì)發(fā)表十七篇文章,上榜七篇。歡迎免費(fèi)訂閱!歡迎大家一起學(xué)習(xí),一起成長??!
????悲索之人烈焰加身,墮落者不可饒恕。永恒燃燒的羽翼,帶我脫離凡間的沉淪。
Scrapy Shell簡介
?? ??Scrapy
是一個(gè)開源的Python框架,用于快速、高效地爬取網(wǎng)站數(shù)據(jù)。Scrapy提供了一組功能強(qiáng)大的工具和組件,使開發(fā)人員可以輕松地從網(wǎng)站上提取所需的數(shù)據(jù)。
?? ??Scrapy Shell
是一個(gè)命令行工具,可以讓開發(fā)人員交互式地調(diào)試和探索網(wǎng)站。使用Scrapy Shell
,開發(fā)人員可以輕松地測試Web爬蟲并查看網(wǎng)站上的數(shù)據(jù)。
?? 以下是Scrapy Shell
的一些主要特點(diǎn):
-
輕松進(jìn)入交互式Shell:只需輸入“scrapy shell”命令即可進(jìn)入交互式Shell。
-
可以直接訪問網(wǎng)站并查看HTML源代碼:使用Scrapy Shell,您可以直接訪問網(wǎng)站并查看其HTML源代碼。這使您可以輕松地查找要提取的數(shù)據(jù)的位置。
-
支持XPath選擇器和CSS選擇器:在Scrapy Shell中,您可以使用XPath或CSS選擇器快速選擇和提取所需的數(shù)據(jù)。
-
可以在Scrapy Shell中測試爬蟲:使用Scrapy Shell,您可以測試和調(diào)試Web爬蟲,以確保其能夠正確提取所需的數(shù)據(jù)。
-
支持調(diào)用各種Python函數(shù)和庫:在Scrapy Shell中,您可以輕松調(diào)用各種Python函數(shù)和庫,以進(jìn)一步處理所提取的數(shù)據(jù)。
Scrapy Shell是一個(gè)非常強(qiáng)大的工具,可以幫助開發(fā)人員快速而方便地開發(fā)和調(diào)試Web爬蟲,提取所需的數(shù)據(jù)。
進(jìn)入shell調(diào)試網(wǎng)站
啟動(dòng)Scrapy Shell
?? ??打開命令行終端,并進(jìn)入一個(gè)Scrapy項(xiàng)目根目錄,輸入以下命令啟動(dòng)Scrapy Shell:
scrapy shell url
??這里以百度為例:
scrapy shell https://www.baidu.com/?tn=02003390_84_hao_pg&
??運(yùn)行結(jié)果:
查看目標(biāo)網(wǎng)站
?? ??使用Scrapy Shell可以查看目標(biāo)網(wǎng)站的內(nèi)容,具體步驟如下:
- 打開命令行終端,在命令行中鍵入以下命令開始進(jìn)入Scrapy Shell:
scrapy shell <url> 其中,`<url>`是目標(biāo)網(wǎng)站的網(wǎng)址。
- 等待一段時(shí)間,直到Scrapy Shell加載完畢,顯示下面的信息:
In [1]:
- 在Scrapy Shell中輸入以下命令,獲取目標(biāo)網(wǎng)站的內(nèi)容:
response.body
- 輸入以下命令可以查看HTTP頭信息:
response.headers
- 輸入以下命令可以查看目標(biāo)網(wǎng)頁的標(biāo)題:
response.xpath('//title/text()').get()
- 如需退出Scrapy Shell,請(qǐng)輸入以下命令:
exit()
view(response)
獲取網(wǎng)站源代碼
response.body
常用方法
?? ??輸入response. 在這個(gè)時(shí)候按下鍵盤上的Tab鍵就可以實(shí)現(xiàn)參數(shù)選擇
response.url # 當(dāng)前響應(yīng)的url地址
response.request.url # 當(dāng)前響應(yīng)對(duì)應(yīng)的請(qǐng)求的url地址
response.text # html字符串,str類型
response.body # 廣義上二進(jìn)制的響應(yīng),bytes類型,相當(dāng)于是response.content
response.body.decode() # 對(duì)bytes類型的字符串進(jìn)行解碼,將其變?yōu)閟tr類型,相當(dāng)于是response.content.decode()
response.xpath() # 調(diào)試我們的xpath表達(dá)式寫地是否正確
response.xpath('//ul[@class="clears"]/li/div[@class="main_mask"]/h2[1]/text()').extract()
response.headers # 響應(yīng)頭
response.request.headers # 當(dāng)前響應(yīng)的請(qǐng)求頭
調(diào)試xpath
??先將文本打印出來
response.text
response.xpath('/html/head/title/text()')
提取數(shù)據(jù)
response.xpath('/html/head/title/text()').extract()
Scrapy請(qǐng)求子頁面
請(qǐng)求及返回處理
創(chuàng)建項(xiàng)目
scrapy startproject douban_movie
創(chuàng)建爬蟲
??進(jìn)入到spiders文件下創(chuàng)建創(chuàng)建爬蟲文件
cd douban_movie\douban_movie\spiders
創(chuàng)建爬蟲
??命令[scrapy genspider 爬蟲的名稱 爬蟲網(wǎng)站]
scrapy genspider movie movie.douban.com
創(chuàng)建成功
數(shù)據(jù)解析
title = response.xpath('normalize-space(string(//div[@id="content"]/h1))').extract()
info = response.xpath('normalize-space(string(//div[@id="info"]))').extract()
import scrapy
class MovieSpider(scrapy.Spider):
name = "movie"
allowed_domains = ["movie.douban.com"]
start_urls = [
f'https://movie.douban.com/j/chart/top_list?type=24&interval_id=100%3A90&action=&start={i}&limit=20'
for i in range(0, 100)]
def parse(self, response, *_):
urls = [i['url']for i in response.json()]
yield from response.follow_all(urls=urls, callback=self.parse_t)
def parse_t(self,response,*_):
title = response.xpath('normalize-space(string(//div[@id="content"]/h1))').extract()
info = response.xpath('normalize-space(string(//div[@id="info"]))').extract()
print(title)
# yield {
# 'title': title,
# 'info': info
# }
zip(title, info)
for i in zip(title, info):
print(i[0], i[1])
yield {
'title': title,
'info': info
}
寫入csv文件
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import csv
class DoubanMoviePipeline:
def __init__(self):
self.f = open('電影.csv', 'w+', encoding='utf', newline='')
self.csv_f = csv.writer(self.f)
self.csv_f.writerow(['標(biāo)題', '簡介'])
def process_item(self, item, spider):
self.csv_f.writerow([item['title'], item['info']])
return item
def close_spider(self):
self.f.close()
print("信息寫入完成!")
文章來源:http://www.zghlxwxcb.cn/news/detail-421464.html
后記
????本專欄所有文章是博主學(xué)習(xí)筆記,僅供學(xué)習(xí)使用,爬蟲只是一種技術(shù),希望學(xué)習(xí)過的人能正確使用它。
博主也會(huì)定時(shí)一周三更爬蟲相關(guān)技術(shù)更大家系統(tǒng)學(xué)習(xí),如有問題,可以私信我,沒有回,那我可能在上課或者睡覺,寫作不易,感謝大家的支持??!??????文章來源地址http://www.zghlxwxcb.cn/news/detail-421464.html
到了這里,關(guān)于18.網(wǎng)絡(luò)爬蟲—Scrapy實(shí)戰(zhàn)演示的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!