当前位置: 首页 > news >正文

做国外网站什么定位产品软文是什么意思

做国外网站什么定位,产品软文是什么意思,中信建设有限责任公司待遇怎么样,专做童车批发的网站目录 一、创建一个scrapy项目 二、xpath解析数据 三、通过pipelines管道实现数据保存 四、中间件 一、创建一个scrapy项目 1.创建一个文件夹:C06 在终端输入以下命令: 2.安装scrapy:pip install scrapy 3.来到文件夹下:cd C06 4.创建…

目录

一、创建一个scrapy项目

二、xpath解析数据

三、通过pipelines管道实现数据保存

四、中间件


一、创建一个scrapy项目

1.创建一个文件夹:C06

在终端输入以下命令:

2.安装scrapy:pip install scrapy

3.来到文件夹下:cd C06

4.创建项目:scrapy startproject C06L02(项目名称)

5.切换到C06L02下:cd C06L02/C06L02

    切换到spiders下:cd spiders

6.创建爬虫名称和输入爬取链接:scrapy genspider app https://product.cheshi.com/rank/2-0-0-0-1/

(若是crawlspider爬虫字类实现全站爬取:scrapy genspider -t crawl app http://seller.cheshi.com/jinan/

7.注意看爬虫文件(新生成的app.py)链接是否一致

8.运行爬虫文件:scrapy crawl app       

9.若想要消除日志文件,在settings.py中添加命令:LOG_LEVEL="ERROR" 

    若想要绕过ROBOTS协议,在settings.py中添加命令:ROBOTSTXT_OBEY=False

10.简单的scrapy项目的app.py文件代码如下:

import scrapyclass AppSpider(scrapy.Spider):name = "app"allowed_domains = ["product.cheshi.com"]started_urls = ["http://product.cheshi.com/rank/2-0-0-0-1/"]def parse(self, response):print(response.text)

若是crawlspider爬虫字类实现全站爬取:

import scrapy
from scrapy.linkextractors import linkExtractor
from scrapy.spiders import CrawlSpider, Ruleclass AppSpider(CrawlSpider):name = "app"allowed_domains = ["product.cheshi.com"]started_urls = ["http://product.cheshi.com/jinan"]rules = (Rule(linkExtractor(allow=r"seller.cheshi.com/\d+", deny=r"seller.cheshi.com/\d+/.+"),callback="parse_item",follow=True),)def parse(self, response):print(response.url)

11.user-agent配置:在settings.py文件中将user-agent注释内容展开,添加需要内容

二、xpath解析数据

在app.py文件中修改parse函数

import scrapyclass AppSpider(scrapy.Spider):name = "app"allowed_domains = ["product.cheshi.com"]started_urls = ["http://product.cheshi.com/rank/2-0-0-0-1/"]def parse(self, response):cars = response.xpath('//ul[@class="condition_list_con"]/li')for car in cars:title = car.xpath('./div[@class="m_detail"]//a/text()').get()price = car.xpath('./div[@class="m_detail"]//b/text()').get()

若实现分页爬取则为以下代码

import scrapy
from ..items import C06L10Itemclass AppSpider(scrapy.Spider):name = "app"allowed_domains = ["book.douban.com"]started_urls = ["http://book.douban.com/latest"]def parse(self, response):books = response.xpath('//ul[@class="chart-dashed-list"]/li')for book in books:link = book.xpath('.//h2/a/@href').get()yield scrapy.Request(url=link,callback=self.parse_details)next_url = response.xpath('//*[@id="content"]/div/div[1]/div[4]/span[4]/a/@href').get()if next_url is not None:next_url = response.urljoin(next_url)print(next_url)yield scrapy.Request(url=next_url, callback=self.parse)else:next_url = response.xpath('//*[@id="content"]/div/div[1]/div[4]/span[3]/a/@href').get()next_url = response.urljoin(next_url)print(next_url)yield scrapy.Request(url=next_url, callback=self.parse)def parse_details(self, reponse):item = C06L10Item()item["title"] = response.xpath('//*[id="wrapper"]/h1/span/text()').get()item["publisher"] = response.xpath('//*[id="info"]/a[1]/text()').get()yield item

三、通过pipelines管道实现数据保存

1.在items.py文件中定义数据模型

import scrapyclass C06L04Item(scrapy.Item):title = scrapy.Field()price = scrapy.Field()

2.在app.py文件中添加如下代码

import scrapy
from ..items import C06L04Itemclass AppSpider(scrapy.Spider):name = "app"allowed_domains = ["product.cheshi.com"]started_urls = ["http://product.cheshi.com/rank/2-0-0-0-1/"]def parse(self, response):item = C06L04Item()cars = response.xpath('//ul[@class="condition_list_con"]/li')for car in cars:item["title"] = car.xpath('./div[@class="m_detail"]//a/text()').get()item["price"] = car.xpath('./div[@class="m_detail"]//b/text()').get()yield item

3.在settings.py文件中展开被注释掉的ITEM_PIPELINES,无需修改

4.修改pipelines.py文件代码

from itemadapter import ItemAdapterclass C06L04Pipeline:def process_item(self, item, spider):# print(item["title"],item["price"])return item

若想要保存成文件添加以下代码

from itemadapter import ItemAdapterclass C06L04Pipeline:def __init__(self):self.f = open("data.tet", "w")def process_item(self, item, spider):self.f.write(item["title"]+item["price"]+"\n")return itemdef __del__(self):self.f.close()

存储为mongodb形式为如下代码

from itemadapter import ItemAdapter
import pymongoclass C06L04Pipeline:def __init__(self):self.client = pymongo.MongoClient("mongodb://localhost:27017")self.db = self.client["cheshi"]self.col = self.db["cars"]def process_item(self, item, spider):res = self.col.insert_one(dict(item))print(res.inserted_id)return itemdef __del__(self):print("end")

四、中间件

1.Middleware的应用:随机User-Agent、代理IP、使用Selenium、添加Cookie

2.动态User-Agent

打开settings.py文件中注释掉的DOWNLOADER_MIDDLEWARES

在middlewares.py文件中添加如下代码(只显示修改部分):

import randomdef process_request(self, request, spider):uas = ["User-Agent:Mxxxxxxxxxxxxxxxxxxxxxxxx","User-Agent:Mxxxxxxxxxxxxxxxxxxxxxxxx","User-Agent:Mxxxxxxxxxxxxxxxxxxxxxxxx","User-Agent:Mxxxxxxxxxxxxxxxxxxxxxxxx",]request.headers["User-Agent"] = random.choice(uas)

2.代理IP

具体操作略去,例如:快代理-隧道代理-python-scrapy的文档中心有具体的书写方式

http://www.hkea.cn/news/547163/

相关文章:

  • 营销型网站建设吉林定制化网站建设
  • 个人网上公司注册流程图新站优化案例
  • 做se要明白网站明星百度指数排名
  • 网页微博草稿箱在哪西安seo推广优化
  • 嘉兴微信网站建设谷歌首页
  • 什么网站做海报b站不收费网站
  • 如何自己做个简单网站seo知识点
  • 有哪些做批发的网站有哪些手续百度推广优化是什么意思
  • 用阿里巴巴店铺做公司网站怎么样引擎搜索有哪些
  • 网页制作软件属于什么软件类别简述seo的优化流程
  • 网站建设 公司新闻谷歌排名网站优化
  • 怎样做自己的vip解析网站佛山外贸seo
  • 我的网站在百度搜不到了seo是什么职业做什么的
  • 网站私信界面国外网站seo免费
  • wordpress mysql类惠州网站seo
  • 为什么做网站必须要用域名举出最新的网络营销的案例
  • 电子请柬网站开发百度竞价推广登录入口
  • 网站设计与推广国际时事新闻2022最新
  • 柬埔寨网站开发营销技巧和营销方法
  • 网站建立价格长沙网站外包公司
  • 王建设医生个人网站免费google账号注册入口
  • 免费自建手机网站搜索引擎优化的方法包括
  • 甘肃省建设工程安全质量监督管理局网站官网拉新项目官方一手平台
  • 做电影网站赚钱武汉新闻最新消息
  • 做网站没有成本的方法上海百度分公司电话
  • 寺庙网站建设百度ai人工智能
  • 完成公司网站建设下载关键词推广软件
  • wordpress如何关闭网站下载app
  • WordPress小程序二次修改石家庄seo排名外包
  • 做百度关键词网站厦门seo外包