Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
30d70c4c57 | 4 years ago |
@ -1,3 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@ -1,12 +0,0 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="ignoredErrors">
|
||||
<list>
|
||||
<option value="N806" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
@ -1,6 +0,0 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/PythonScrapyWeather.iml" filepath="$PROJECT_DIR$/.idea/PythonScrapyWeather.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,17 +0,0 @@
|
||||
# Define here the models for your scraped items
|
||||
#
|
||||
# See documentation in:
|
||||
# https://docs.scrapy.org/en/latest/topics/items.html
|
||||
|
||||
import scrapy
|
||||
|
||||
|
||||
class PythonscrapyweatherItem(scrapy.Item):
|
||||
# define the fields for your item here like:
|
||||
# name = scrapy.Field()
|
||||
province_Name = scrapy.Field()
|
||||
city_Name = scrapy.Field()
|
||||
date = scrapy.Field()
|
||||
temperature = scrapy.Field()
|
||||
weather_condition = scrapy.Field()
|
||||
air_quality = scrapy.Field()
|
||||
@ -1,70 +0,0 @@
|
||||
# Define your item pipelines here
|
||||
#
|
||||
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
|
||||
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
|
||||
|
||||
|
||||
# useful for handling different item types with a single interface
|
||||
from itemadapter import ItemAdapter
|
||||
import pymysql
|
||||
|
||||
class PythonscrapyweatherPipeline(object):
|
||||
# 连接数据库
|
||||
def __init__(self, settings):
|
||||
# self.connect = pymysql.connect(
|
||||
# host='localhost',
|
||||
# port=3306,
|
||||
# db='datasave_sql',
|
||||
# user='root',
|
||||
# password='123456',
|
||||
# charset="utf8",
|
||||
# use_unicode=False)
|
||||
# # 通过cursor执行增删查改
|
||||
# self.cursor = self.connect.cursor()
|
||||
# self.cursor.execute("SELECT VERSION()")
|
||||
self.settings = settings
|
||||
print("连接成功")
|
||||
|
||||
def process_item(self, item, spider):
|
||||
print("开始插入")
|
||||
# 插入数据库
|
||||
sql = '''INSERT INTO weathers(city_Name,date,temperature,weather_condition,air_quality)
|
||||
VALUES("{}","{}","{}","{}","{}")'''
|
||||
try:
|
||||
self.cursor.execute(sql.format(
|
||||
# pymysql.converters.escape_string("1"),
|
||||
pymysql.converters.escape_string(item["city_Name"]),
|
||||
pymysql.converters.escape_string(item["date"]),
|
||||
pymysql.converters.escape_string(item["temperature"]),
|
||||
pymysql.converters.escape_string(item["weather_condition"]),
|
||||
pymysql.converters.escape_string(item["air_quality"])))
|
||||
self.connect.commit()
|
||||
print(self.cursor.rowcount, "记录插入成功。")
|
||||
except BaseException as e:
|
||||
print("错误在这里>>>>>>>>>>>>>", e, "<<<<<<<<<<<<<错误在这里")
|
||||
self.connect.rollback()
|
||||
return item
|
||||
|
||||
@classmethod
|
||||
def from_crawler(cls, crawler):
|
||||
return cls(crawler.settings)
|
||||
|
||||
def open_spider(self, spider):
|
||||
# 连接数据库
|
||||
self.connect = pymysql.connect(
|
||||
host=self.settings.get('MYSQL_HOST'),
|
||||
port=self.settings.get('MYSQL_PORT'),
|
||||
db=self.settings.get('MYSQL_DBNAME'),
|
||||
user=self.settings.get('MYSQL_USER'),
|
||||
passwd=self.settings.get('MYSQL_PASSWD'),
|
||||
charset='utf8',
|
||||
use_unicode=True)
|
||||
|
||||
# 通过cursor执行增删查改
|
||||
self.cursor = self.connect.cursor();
|
||||
self.connect.autocommit(True)
|
||||
|
||||
# 关闭数据库
|
||||
def close_spider(self, spider):
|
||||
self.cursor.close()
|
||||
self.connect.close()
|
||||
@ -1,110 +0,0 @@
|
||||
# Scrapy settings for PythonScrapyWeather project
|
||||
#
|
||||
# For simplicity, this file contains only settings considered important or
|
||||
# commonly used. You can find more settings consulting the documentation:
|
||||
#
|
||||
# https://docs.scrapy.org/en/latest/topics/settings.html
|
||||
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
|
||||
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
|
||||
|
||||
BOT_NAME = 'PythonScrapyWeather'
|
||||
|
||||
SPIDER_MODULES = ['PythonScrapyWeather.spiders']
|
||||
NEWSPIDER_MODULE = 'PythonScrapyWeather.spiders'
|
||||
|
||||
|
||||
# Crawl responsibly by identifying yourself (and your website) on the user-agent
|
||||
#USER_AGENT = 'PythonScrapyWeather (+http://www.yourdomain.com)'
|
||||
|
||||
# Obey robots.txt rules
|
||||
ROBOTSTXT_OBEY = True
|
||||
|
||||
# Configure maximum concurrent requests performed by Scrapy (default: 16)
|
||||
#CONCURRENT_REQUESTS = 32
|
||||
|
||||
# Configure a delay for requests for the same website (default: 0)
|
||||
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
|
||||
# See also autothrottle settings and docs
|
||||
#DOWNLOAD_DELAY = 3
|
||||
# The download delay setting will honor only one of:
|
||||
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
|
||||
#CONCURRENT_REQUESTS_PER_IP = 16
|
||||
|
||||
# Disable cookies (enabled by default)
|
||||
#COOKIES_ENABLED = False
|
||||
|
||||
# Disable Telnet Console (enabled by default)
|
||||
#TELNETCONSOLE_ENABLED = False
|
||||
|
||||
# Override the default request headers:
|
||||
#DEFAULT_REQUEST_HEADERS = {
|
||||
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
# 'Accept-Language': 'en',
|
||||
#}
|
||||
|
||||
# Enable or disable spider middlewares
|
||||
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
|
||||
#SPIDER_MIDDLEWARES = {
|
||||
# 'PythonScrapyWeather.middlewares.PythonscrapyweatherSpiderMiddleware': 543,
|
||||
#}
|
||||
|
||||
# Enable or disable downloader middlewares
|
||||
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
|
||||
#DOWNLOADER_MIDDLEWARES = {
|
||||
# 'PythonScrapyWeather.middlewares.PythonscrapyweatherDownloaderMiddleware': 543,
|
||||
#}
|
||||
|
||||
# Enable or disable extensions
|
||||
# See https://docs.scrapy.org/en/latest/topics/extensions.html
|
||||
#EXTENSIONS = {
|
||||
# 'scrapy.extensions.telnet.TelnetConsole': None,
|
||||
#}
|
||||
|
||||
# Configure item pipelines
|
||||
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
|
||||
#ITEM_PIPELINES = {
|
||||
# 'PythonScrapyWeather.pipelines.PythonscrapyweatherPipeline': 300,
|
||||
#}
|
||||
|
||||
# Enable and configure the AutoThrottle extension (disabled by default)
|
||||
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
|
||||
#AUTOTHROTTLE_ENABLED = True
|
||||
# The initial download delay
|
||||
#AUTOTHROTTLE_START_DELAY = 5
|
||||
# The maximum download delay to be set in case of high latencies
|
||||
#AUTOTHROTTLE_MAX_DELAY = 60
|
||||
# The average number of requests Scrapy should be sending in parallel to
|
||||
# each remote server
|
||||
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
|
||||
# Enable showing throttling stats for every response received:
|
||||
#AUTOTHROTTLE_DEBUG = False
|
||||
|
||||
# Enable and configure HTTP caching (disabled by default)
|
||||
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
|
||||
#HTTPCACHE_ENABLED = True
|
||||
#HTTPCACHE_EXPIRATION_SECS = 0
|
||||
#HTTPCACHE_DIR = 'httpcache'
|
||||
#HTTPCACHE_IGNORE_HTTP_CODES = []
|
||||
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
|
||||
# 设置下载中间件
|
||||
DOWNLOADER_MIDDLEWARES = {
|
||||
'PythonScrapyWeather.middlewares.PythonscrapyweatherDownloaderMiddleware': 543,
|
||||
}
|
||||
# 设置请求头
|
||||
DEFAULT_REQUEST_HEADERS = {
|
||||
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
|
||||
}
|
||||
# 设置COOKIE
|
||||
COOKIES_ENABLED = True
|
||||
# 设置代理池
|
||||
# IP_PROXY
|
||||
|
||||
ITEM_PIPELINES = {
|
||||
'PythonScrapyWeather.pipelines.PythonscrapyweatherPipeline': 300,
|
||||
}
|
||||
|
||||
MYSQL_HOST = 'localhost'
|
||||
MYSQL_DBNAME = 'datasave_sql'
|
||||
MYSQL_USER = 'root'
|
||||
MYSQL_PASSWD = '123456'
|
||||
MYSQL_PORT = 3306
|
||||
@ -1,4 +0,0 @@
|
||||
# This package will contain the spiders of your Scrapy project
|
||||
#
|
||||
# Please refer to the documentation for information on how to create and manage
|
||||
# your spiders.
|
||||
Binary file not shown.
Binary file not shown.
@ -1,11 +0,0 @@
|
||||
# Automatically created by: scrapy startproject
|
||||
#
|
||||
# For more information about the [deploy] section see:
|
||||
# https://scrapyd.readthedocs.io/en/latest/deploy.html
|
||||
|
||||
[settings]
|
||||
default = PythonScrapyWeather.settings
|
||||
|
||||
[deploy]
|
||||
#url = http://localhost:6800/
|
||||
project = PythonScrapyWeather
|
||||
Loading…
Reference in new issue