技术标签: 爬虫 阿里云 python Python爬虫 Python之路 scrapy
目录
本文章仅用于学习交流,无任何商业用途
这次我们要学习把爬取到的数据存入数据库之中
settings中添加下面的内容
# todo 配置 mysql数据库 # 这里是我的阿里云地址,你填你mysql的地址 DB_HOST = 'xx.xx.xx.xx' DB_PORT = 3306 DB_USER = 'root' DB_PASSWORD = '12345678' DB_NAME = 'spider01' DB_CHARSET = 'utf-8'
添加下面的代码
class MysqlPipeline:
def process_item(self, item, spider):
return item
再添加配置
ITEM_PIPELINES = { "scrapy_readbook_090.pipelines.ScrapyReadbook090Pipeline": 300, # MysqlPipeline "scrapy_readbook_090.pipelines.MysqlPipeline": 301 }
。。。。
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from scrapy_readbook_090.items import ScrapyReadbook090Item
class ReadSpider(CrawlSpider):
name = "read"
allowed_domains = ["www.dushu.com"]
start_urls = ["https://www.dushu.com/book/1188_1.html"]
rules = (Rule(LinkExtractor(allow=r"/book/1188_\d+\.html"),
callback="parse_item",
# true代表是否跟进
# 打开follow为true就会爬取全部网页
follow=True),)
def parse_item(self, response):
img_list = response.xpath('//div[@class="bookslist"]//img')
for img in img_list:
name = img.xpath('./@alt').extract_first()
img_src = img.xpath('./@data-original').extract_first()
book = ScrapyReadbook090Item(name=name, src=img_src)
yield book
# 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
class ScrapyReadbook090Pipeline:
def open_spider(self, spider):
self.fp = open('book.json', 'w', encoding='utf-8')
def process_item(self, item, spider):
self.fp.write(str(item))
return item
def close_spider(self, spider):
self.fp.close()
# 加载settings文件
from scrapy.utils.project import get_project_settings
import pymysql
class MysqlPipeline:
def open_spider(self, spider):
settings = get_project_settings()
self.host = settings['DB_HOST']
self.port = settings['DB_PORT']
self.user = settings['DB_USER']
self.password = settings['DB_PASSWORD']
self.name = settings['DB_NAME']
self.charset = settings['DB_CHARSET']
self.connect()
def connect(self):
self.conn = pymysql.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
db=self.name,
charset=self.charset
)
# 可执行sql语句
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
sql = 'insert into book2(name,src) values("{}","{}")'.format(item['name'], item['src'])
# 执行SQL语句
self.cursor.execute(sql)
# 提交
self.conn.commit()
return item
def close_spider(self, spider):
self.cursor.close()
self.conn.close()
# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import scrapy
class ScrapyReadbook090Item(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
src = scrapy.Field()
# Scrapy settings for scrapy_readbook_090 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 = "scrapy_readbook_090"
SPIDER_MODULES = ["scrapy_readbook_090.spiders"]
NEWSPIDER_MODULE = "scrapy_readbook_090.spiders"
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = "scrapy_readbook_090 (+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 = {
# "scrapy_readbook_090.middlewares.ScrapyReadbook090SpiderMiddleware": 543,
#}
# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# "scrapy_readbook_090.middlewares.ScrapyReadbook090DownloaderMiddleware": 543,
#}
# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# "scrapy.extensions.telnet.TelnetConsole": None,
#}
# todo 配置 mysql数据库
DB_HOST = '8.137.20.36'
# 端口号要是整形
DB_PORT = 3306
DB_USER = 'root'
DB_PASSWORD = '12345678'
DB_NAME = 'spider01'
# utf-8的 - 不要写
DB_CHARSET = 'utf8'
# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
"scrapy_readbook_090.pipelines.ScrapyReadbook090Pipeline": 300,
# MysqlPipeline
"scrapy_readbook_090.pipelines.MysqlPipeline": 301
}
# 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"
# Set settings whose default value is deprecated to a future-proof value
REQUEST_FINGERPRINTER_IMPLEMENTATION = "2.7"
TWISTED_REACTOR = "twisted.internet.asyncioreactor.AsyncioSelectorReactor"
FEED_EXPORT_ENCODING = "utf-8"
最后是找到了4000条数据
可能是io进服务器的顺序问题,军娃不是最后一个,但是一页40本书,一共100页也是没有一点毛病了。(* ^ ▽ ^ *)
Scrapy是一个基于Python的网络爬虫框架,它提供了强大的日志功能。Scrapy的日志信息以及日志级别如下:
DEBUG:调试级别,用于输出详细的调试信息,一般在开发和测试阶段使用。
INFO:信息级别,用于输出一些重要的信息,如爬虫的启动信息、请求的URL等。
WARNING:警告级别,用于输出一些不太严重的警告信息,如某个网页的解析出错,但不影响整个爬虫的执行。
ERROR:错误级别,用于输出一些错误信息,如爬虫的配置出错、网络连接异常等。
CRITICAL:严重级别,用于输出一些非常严重的错误信息,如爬虫的关键逻辑出错、无法连接到目标网站等。
默认的日志级别是DEBUG
Scrapy的日志信息可以在控制台中直接输出,也可以保存到文件中。可以通过设置Scrapy的配置文件或使用命令行参数来调整日志级别和输出方式。
以下是Scrapy的日志信息的示例:
2021-01-01 12:00:00 [scrapy.core.engine] INFO: Spider opened
2021-01-01 12:00:01 [scrapy.core.engine] DEBUG: Crawled 200 OK
2021-01-01 12:00:01 [scrapy.core.engine] DEBUG: Crawled 404 Not Found
2021-01-01 12:00:02 [scrapy.core.engine] WARNING: Ignoring response <404 Not Found>
2021-01-01 12:00:02 [scrapy.core.engine] DEBUG: Crawled 200 OK
2021-01-01 12:00:02 [scrapy.core.engine] ERROR: Spider error processing <GET http://example.com>: Error parsing HTML
2021-01-01 12:00:03 [scrapy.core.engine] DEBUG: Crawled 200 OK
2021-01-01 12:00:03 [scrapy.core.engine] INFO: Closing spider (finished)
2021-01-01 12:00:03 [scrapy.statscollectors] INFO: Dumping Scrapy stats
默认的级别是DEBUG,会显示上面的所有信息
在配置文件中 settings.py
LOG_FILE : 将屏幕显示的信息全部记录到文件中,屏幕不再显示,注意文件后最有一定是 .log
LOG_LEVEL : 设置日志的等级,就是显示那些,不显示那些
先把 “君子协议” 撕碎
# ROBOTSTXT_OBEY = True
在settings.py中添加下述代码
# 指定日志的级别
LOG_LEVEL = 'WARNING'
==========是我在log.py中添加要打印的
就可以发现没有日志了
我们先把上面配置的等级删除掉,再加上下述的代码
# 日志文件
LOG_FILE = 'logDemo.log'
运行
世界依然清晰
但是日志已经存储在日志文件中了
其实一般来说不要修改log的等级,如果报错也太难发现是什么问题了,所以一般为了控制台别打印那么多东西
在Scrapy中进行POST请求可以通过scrapy.FormRequest
类来实现。下面是一个使用Scrapy进行POST请求的示例:
import scrapy
class MySpider(scrapy.Spider):
name = 'example.com'
start_urls = ['http://www.example.com/login']
def parse(self, response):
# 提取登录页的csrf token
csrf_token = response.css('input[name="csrf_token"]::attr(value)').get()
# 构建POST请求的表单数据
formdata = {
'username': 'myusername',
'password': 'mypassword',
'csrf_token': csrf_token
}
# 发送POST请求
yield scrapy.FormRequest(url='http://www.example.com/login', formdata=formdata, callback=self.after_login)
def after_login(self, response):
# 检查登录是否成功
if response.url == 'http://www.example.com/home':
self.log('Login successful')
# 处理登录成功后的响应数据
# ...
else:
self.log('Login failed')
在上面的示例中,首先在parse
方法中抓取登录页,并提取登录页的csrf token。然后构建一个包含用户名、密码和csrf token的字典,作为formdata
参数传递给FormRequest
对象。最后使用yield
关键字发送POST请求,并指定回调函数after_login
来处理登录后的响应。
在after_login
方法中,可以根据响应的URL来判断登录是否成功。如果URL为登录后的首页URL,则登录成功,否则登录失败。可以在登录成功时做进一步的处理,如抓取用户信息,然后在控制台或日志中输出相应的信息。
需要注意的是,Scrapy的POST请求默认使用application/x-www-form-urlencoded
方式来编码数据。如果需要发送JSON或其他类型的请求,可以通过设置headers
参数来指定请求头,如:yield scrapy.FormRequest(url='http://www.example.com/login', formdata=formdata, headers={'Content-Type': 'application/json'}, callback=self.after_login)
。
另外,如果需要在POST请求中上传文件,可以使用scrapy.FormRequest
的files
参数,将文件的路径作为值传递给表单字段。更多关于POST请求的用法和参数配置,请查阅Scrapy官方文档。
只需要修改testpost.py这个自己创建的文件就行了
import scrapy
import json
class TestpostSpider(scrapy.Spider):
name = "testpost"
allowed_domains = ["fanyi.baidu.com"]
# post请求如果没有参数,那抹这个请求将没有任何的意义
# 所以 start_urls 也是没有用
# 而且 parse 方法也没有用了
# 所以直接注释掉
# TODO
# start_urls = ["https://fanyi.baidu.com/sug"]
#
# def parse(self, response):
# print("==========================")
# post请求就使用这个方法
def start_requests(self):
url = 'https://fanyi.baidu.com/sug'
data = {
'kw': 'final'
}
yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse_second)
def parse_second(self, response):
content = response.text
obj = json.loads(content, encoding='utf-8')
print(obj)
从2月29号,到今天3月9号,一共过去了十天,完成了爬虫的入门,从urllib到scrapy,这条路很长但是也很简单,中间的配置Python软件包的版本问题时常可以阻碍我的脚步,但是我都一一将他们解决,困难毕竟只是困难,人定胜天,我命由我不由天,加油!!!ヾ(◍°∇°◍)ノ゙
ヾ( ̄▽ ̄)Bye~Bye~
完结撒花
文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr
文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc
文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8
文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束
文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求
文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname
文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#include<iostream>#include<stack>#include<queue>using namespace std;typed_二叉树的建立
文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码
文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词
文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限
文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定
文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland