摘要:安裝可能的問題問題解決實例教程中文教程文檔第一步創(chuàng)建項目目錄第二步進入創(chuàng)建爬蟲第三步創(chuàng)建存儲容器,復制項目下的重命名為第四步修改提取數(shù)據(jù)引入數(shù)據(jù)容器第五步解決百度首頁網(wǎng)站抓取空白問題設(shè)置設(shè)置用戶代理解決相關(guān)解決數(shù)據(jù)保存亂
pip 安裝 pip install scrapy
可能的問題:
問題/解決:error: Microsoft Visual C++ 14.0 is required.
實例demo教程 中文教程文檔
第一步:創(chuàng)建項目目錄
scrapy startproject tutorial
第二步:進入tutorial創(chuàng)建spider爬蟲
scrapy genspider baidu www.baidu.com
第三步:創(chuàng)建存儲容器,復制項目下的items.py重命名為BaiduItems
# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.html import scrapy class BaiduItems(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() title = scrapy.Field() link = scrapy.Field() desc = scrapy.Field() pass
第四步:修改spiders/baidu.py xpath提取數(shù)據(jù)
# -*- coding: utf-8 -*- import scrapy # 引入數(shù)據(jù)容器 from tutorial.BaiduItems import BaiduItems class BaiduSpider(scrapy.Spider): name = "baidu" allowed_domains = ["www.readingbar.net"] start_urls = ["http://www.readingbar.net/"] def parse(self, response): for sel in response.xpath("http://ul/li"): item = BaiduItems() item["title"] = sel.xpath("a/text()").extract() item["link"] = sel.xpath("a/@href").extract() item["desc"] = sel.xpath("text()").extract() yield item pass
第五步:解決百度首頁網(wǎng)站抓取空白問題,設(shè)置setting.py
# 設(shè)置用戶代理 USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36" # 解決 robots.txt 相關(guān)debug ROBOTSTXT_OBEY = False # scrapy 解決數(shù)據(jù)保存亂碼問題 FEED_EXPORT_ENCODING = "utf-8"
最后一步:開始爬取數(shù)據(jù)命令并保存數(shù)據(jù)為指定的文件
執(zhí)行的時候可能報錯:No module named "win32api" 可以下載指定版本安裝
scrapy crawl baidu -o baidu.json
深度爬取百度首頁及導航菜單相關(guān)頁內(nèi)容
# -*- coding: utf-8 -*- import scrapy from scrapyProject.BaiduItems import BaiduItems class BaiduSpider(scrapy.Spider): name = "baidu" # 由于tab包含其他域名,需要添加域名否則無法爬取 allowed_domains = [ "www.baidu.com", "v.baidu.com", "map.baidu.com", "news.baidu.com", "tieba.baidu.com", "xueshu.baidu.com" ] start_urls = ["https://www.baidu.com/"] def parse(self, response): item = BaiduItems() item["title"] = response.xpath("http://title/text()").extract() yield item for sel in response.xpath("http://a[@class="mnav"]"): item = BaiduItems() item["nav"] = sel.xpath("text()").extract() item["href"] = sel.xpath("@href").extract() yield item # 根據(jù)提取的nav地址建立新的請求并執(zhí)行回調(diào)函數(shù) yield scrapy.Request(item["href"][0],callback=self.parse_newpage) pass # 深度提取tab網(wǎng)頁標題信息 def parse_newpage(self, response): item = BaiduItems() item["title"] = response.xpath("http://title/text()").extract() yield item pass
繞過登錄進行爬取
a.解決圖片驗證 pytesseract
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/42720.html
摘要:所以如果對爬蟲有一定基礎(chǔ),上手框架是一種好的選擇。缺少包,使用安裝即可缺少包,使用安裝即可上一篇文章網(wǎng)絡(luò)爬蟲實戰(zhàn)爬取相關(guān)庫的安裝的安裝下一篇文章網(wǎng)絡(luò)爬蟲實戰(zhàn)爬蟲框架的安裝 上一篇文章:Python3網(wǎng)絡(luò)爬蟲實戰(zhàn)---9、APP爬取相關(guān)庫的安裝:Appium的安裝下一篇文章:Python3網(wǎng)絡(luò)爬蟲實戰(zhàn)---11、爬蟲框架的安裝:ScrapySplash、ScrapyRedis 我們直接...
摘要:時間永遠都過得那么快,一晃從年注冊,到現(xiàn)在已經(jīng)過去了年那些被我藏在收藏夾吃灰的文章,已經(jīng)太多了,是時候把他們整理一下了。那是因為收藏夾太亂,橡皮擦給設(shè)置私密了,不收拾不好看呀。 ...
摘要:楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務,現(xiàn)整理出零基礎(chǔ)如何學爬蟲技術(shù)以供學習,。本文來源知乎作者路人甲鏈接楚江數(shù)據(jù)提供網(wǎng)站數(shù)據(jù)采集和爬蟲軟件定制開發(fā)服務,服務范圍涵蓋社交網(wǎng)絡(luò)電子商務分類信息學術(shù)研究等。 楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務,現(xiàn)整理出零基礎(chǔ)如何學爬蟲技術(shù)以供學習,http://www.chujiangdata.com。 第一:Python爬蟲學習系列教程(來源于某博主:htt...
摘要:想辦法區(qū)分爬蟲程序和正常的用戶。爬蟲是工具性程序,對速度和效率要求較高。生態(tài)圈完善,是最大對手。最要命的是爬蟲需要經(jīng)常修改部分代碼。爬蟲分類通用爬蟲也就是百度搜狐等搜索引擎。原本是為測試來測試網(wǎng)站的,后來成了爬蟲工程師最喜愛的工具。 一、爬蟲的基本知識: 1. 什么是爬蟲 爬蟲的英文翻譯為spider或者crawder,意為蜘蛛或者爬行者,從字面意思我們可以體會到:爬蟲就是把自己當做蜘...
閱讀 3220·2021-11-15 18:14
閱讀 1853·2021-09-22 10:51
閱讀 3364·2021-09-09 09:34
閱讀 3583·2021-09-06 15:02
閱讀 1122·2021-09-01 11:40
閱讀 3251·2019-08-30 13:58
閱讀 2583·2019-08-30 11:04
閱讀 1152·2019-08-28 18:31