成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

【python爬蟲學(xué)習(xí) 】python3.7 scrapy 安裝,demo實(shí)例,實(shí)踐:爬取百度

asoren / 3598人閱讀

摘要:安裝可能的問題問題解決實(shí)例教程中文教程文檔第一步創(chuàng)建項(xiàng)目目錄第二步進(jìn)入創(chuàng)建爬蟲第三步創(chuàng)建存儲(chǔ)容器,復(fù)制項(xià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.

實(shí)例demo教程 中文教程文檔
第一步:創(chuàng)建項(xiàng)目目錄

scrapy startproject tutorial

第二步:進(jìn)入tutorial創(chuàng)建spider爬蟲

scrapy genspider baidu www.baidu.com

第三步:創(chuàng)建存儲(chǔ)容器,復(fù)制項(xià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í)行的時(shí)候可能報(bào)錯(cuò):No module named "win32api" 可以下載指定版本安裝

scrapy crawl baidu -o baidu.json

深度爬取百度首頁及導(dǎo)航菜單相關(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)頁標(biāo)題信息
    def parse_newpage(self, response):
        item = BaiduItems()
        item["title"] = response.xpath("http://title/text()").extract()
        yield item
        pass

繞過登錄進(jìn)行爬取
a.解決圖片驗(yàn)證 pytesseract

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/42720.html

相關(guān)文章

  • Python3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)---10、爬蟲框架的安裝:PySpider、Scrapy

    摘要:所以如果對爬蟲有一定基礎(chǔ),上手框架是一種好的選擇。缺少包,使用安裝即可缺少包,使用安裝即可上一篇文章網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)爬取相關(guān)庫的安裝的安裝下一篇文章網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)爬蟲框架的安裝 上一篇文章:Python3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)---9、APP爬取相關(guān)庫的安裝:Appium的安裝下一篇文章:Python3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)---11、爬蟲框架的安裝:ScrapySplash、ScrapyRedis 我們直接...

    張憲坤 評論0 收藏0
  • 首次公開,整理12年積累的博客收藏夾,零距離展示《收藏夾吃灰》系列博客

    摘要:時(shí)間永遠(yuǎn)都過得那么快,一晃從年注冊,到現(xiàn)在已經(jīng)過去了年那些被我藏在收藏夾吃灰的文章,已經(jīng)太多了,是時(shí)候把他們整理一下了。那是因?yàn)槭詹貖A太亂,橡皮擦給設(shè)置私密了,不收拾不好看呀。 ...

    Harriet666 評論0 收藏0
  • 零基礎(chǔ)如何學(xué)爬蟲技術(shù)

    摘要:楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲技術(shù)以供學(xué)習(xí),。本文來源知乎作者路人甲鏈接楚江數(shù)據(jù)提供網(wǎng)站數(shù)據(jù)采集和爬蟲軟件定制開發(fā)服務(wù),服務(wù)范圍涵蓋社交網(wǎng)絡(luò)電子商務(wù)分類信息學(xué)術(shù)研究等。 楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲技術(shù)以供學(xué)習(xí),http://www.chujiangdata.com。 第一:Python爬蟲學(xué)習(xí)系列教程(來源于某博主:htt...

    KunMinX 評論0 收藏0
  • python爬蟲入門(一)

    摘要:想辦法區(qū)分爬蟲程序和正常的用戶。爬蟲是工具性程序,對速度和效率要求較高。生態(tài)圈完善,是最大對手。最要命的是爬蟲需要經(jīng)常修改部分代碼。爬蟲分類通用爬蟲也就是百度搜狐等搜索引擎。原本是為測試來測試網(wǎng)站的,后來成了爬蟲工程師最喜愛的工具。 一、爬蟲的基本知識: 1. 什么是爬蟲 爬蟲的英文翻譯為spider或者crawder,意為蜘蛛或者爬行者,從字面意思我們可以體會(huì)到:爬蟲就是把自己當(dāng)做蜘...

    lentrue 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<