输入关键字 抓取所有网页(爬取一下京东的商品列表,下拉一下才可以拿到60个 )

优采云 发布时间: 2022-02-22 13:18

  输入关键字 抓取所有网页(爬取一下京东的商品列表,下拉一下才可以拿到60个

)

  今天,我们将爬取京东产品列表。您无需为此 网站 保持登录状态,但列表中有 60 个项目。您必须下拉才能获得 60 件物品。今天,以京东手机为例,我们将涵盖所有这些。抓住

  

  使用xpath获取并尝试一下,我们会看到30个加载并渲染,剩下的30个没有渲染。我们往下拉,会发现30变成了60,剩下的30是动态加载的

  

  对于这个解决方案,我们选择使用 selenium 来下拉和加载它。我们使用scrapy来实现它。我们主要练习技术。有很多方法可以实现它。我们首先创建一个scrapy项目,scrapy startproject JD,cd到目录下,我们在创建一个spider文件,执行scrapy genspider dj,用pycharm打开,

  先写item,确定我们要抓取的字段

  import scrapy

class JdItem(scrapy.Item):

# define the fields for your item here like:

# name = scrapy.Field()

# 手机名称

name = scrapy.Field()

# 链接

link = scrapy.Field()

# 价格

price = scrapy.Field()

# 评论数

comment_num = scrapy.Field()

# 店铺名称

shop_name = scrapy.Field()

# 店铺链接

shop_link = scrapy.Field()

  让我们编写中间件,其中我们使用 selenium 下拉到底部并加载它们。我们必须在 MiddleWare.py 和 scrapy.http 中编写 HtmlResponse 模块。我们使用 HtmlResponse 将源代码返回给蜘蛛进行解析和渲染。记得自己配置webdriver。executable_path 写入路径后的值

  import time

from selenium import webdriver

from scrapy.http import HtmlResponse

class SeleniumWare(object):

def process_request(self,spider,request):

self.option = webdriver.ChromeOptions()

self.option.add_argument("--headless")

self.driver = webdriver.Chrome(executable_path='C:\Program Files\Google\Chrome\Application\chromedriver.exe',options=self.option)

self.driver.get(request.url)

self.driver.implicitly_wait(10)

self.driver.execute_script('var p = document.documentElement.scrollTop=100000')

time.sleep(3)

data = self.driver.page_source

self.driver.close()

data = str(data)

res = HtmlResponse(body=data,encoding="utf-8",request=request,url=request.url)

return res

  编写蜘蛛爬虫

<p># -*- coding: utf-8 -*-

import scrapy

from JD.items import JdItem

class JdSpider(scrapy.Spider):

name = &#39;jd&#39;

allowed_domains = [&#39;jd.com&#39;]

start_urls = [&#39;https://search.jd.com/Search?keyword=手机&#39;]

page = 2

def parse(self, response):

# 获取节点

node_list = response.xpath(&#39;//div[@class="gl-i-wrap"]&#39;)

# 打印个数

print(len(node_list))

# 拿出节点每个信息

for node in node_list:

item = JdItem()

# 我们try一下,有些缺失的抛错,我们阻止异常,返回None

try:

item["name"] = node.xpath(&#39;./div[4]/a/em/text()&#39;).extract_first().strip()

except:

item["name"] = None

try:

item["link"] = response.urljoin(node.xpath(&#39;./div[4]/a/@href&#39;).extract_first())

except:

item["link"] = None

try:

item["price"] = node.xpath(&#39;./div[3]/strong/i/text()&#39;).extract_first() + &#39;元&#39;

except:

item["price"] = None

try:

item["comment_num"] = node.xpath(&#39;./div[5]/strong/a/text()&#39;).extract_first()

except:

item["comment_num"] = None

try:

item["shop_name"] = node.xpath(&#39;./div[7]/span/a/text()&#39;).extract_first().strip()

except:

item["shop_name"] = None

try:

item["shop_link"] = "https:" + node.xpath(&#39;./div[7]/span/a/@href&#39;).extract_first()

except:

item["shop_link"] = None

print(item)

# 返回item,交给pipline

yield item

# 采用拼接的方式获取下一页

if self.page

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线