网站搭建功能需求,只做彩票网站犯法吗,外贸展示型网站建设公司,网站建设可以先备案嘛1、安装scrapy
2、使用scrapy创建项目,在终端命令行 执行如下命令#xff0c;会创建一个myproject项目 scrapy startproject myproject 3、创建完成后#xff0c;目录结构如下 4、cd myproject进入项目 ,执行scrapy genspider weather ******#xff0c;会在spiders下创建…1、安装scrapy
2、使用scrapy创建项目,在终端命令行 执行如下命令会创建一个myproject项目 scrapy startproject myproject 3、创建完成后目录结构如下 4、cd myproject进入项目 ,执行scrapy genspider weather ******会在spiders下创建一个ptyhon文件 scrapy genspider weather weather***** 5、这个文件里面就可以写具体的爬虫逻辑了
import scrapy
import re
from myproject.items import MyprojectItemclass WeatherSpider(scrapy.Spider):# 名称name weather# 爬取域名的范围allowed_domains [******]# 爬取的网址start_urls [********]#start_urls [********]def parse(self, response, **kwargs):data response.xpath(//div[classcontent_l]/dl)for each in data:# 图片img each.xpath(./dt/a/img/src).get()# 标题title each.xpath(./dd/h3/a/text()).get()# 时间create_time each.xpath(./dd/h3/span/text()).get()# 简介description each.xpath(./dd/p/text()).get()content_href each.xpath(./dd/h3/a/href).get()# # 内容链接# item MyprojectItem(# imgimg,# titletitle,# create_timecreate_time,# descriptiondescription,# content_hrefcontent_href# )## yield item# 定义一个回调函数来处理每个链接的响应yield scrapy.Request(urlcontent_href, callbackself.parse_content_page,meta{item: {img: img, title: title, create_time: create_time,description: description}})def parse_content_page(self, response):# print(response.url)# response.meta[item].copy()element response.xpath(//div[classarticleBody]).get()content re.search(rdiv classarticleBody(.*?)/div, element, re.DOTALL).group(1)# 使用正则表达式去除注释内容content re.sub(r!--.*?--, , content)item_data response.meta[item]# print(item_data)# 创建item并填充数据item MyprojectItem(imgitem_data[img],titleitem_data[title],create_timeitem_data[create_time],descriptionitem_data[description],contentcontent # 假设这里添加了从页面中提取的内容)# Yield填充完毕的itemyield item