python学习之第十八天

知识点目录
1.如何多表查询?有几种方式,有什么区别?
2.小知识点
3.主动退出爬虫的两种形式
4.如何实现定时爬虫
5.独立将起点中文网的分析过程和实现方式做一遍

1.如何多表查询?有几种方式,有什么区别?

1.双表查询 (如两个表有个相同的id列)

# 跨表查询
# left join 左连接   左表为主,右表为辅
# right join 右连接   右表为主 左表为辅
# inner join 内连接     取两个表的共同部分
# sql1 = """select * from novel where novel_title='精灵掌门人'"""
# sql1 = """select * from section where novel_href like 'https://book.qidian.com/info/1015792398#Catalog'"""
sql1 = """select * from novel left join section on novel.novel_href=section.novel_href where novel_title='精灵掌门人'"""
"""
stu
id name age
1 张三  20
2 李四  25
3 王五  15
6 赵六  18
score
id subject score
1  数学    95
1  语文    90
1  英语    85
2  数学    95
3  数学    100
4  英语    50
select * from stu left join score on stu.id=score.id
查到4个人,但赵六没有成绩
select * from stu right join score on stu.id=score.id
查到4个人的成绩,但没有赵六成绩,且第4个人未知
select * from stu inner join score on stu.id=score.id
只能查到三个人的成绩

"""

三表查询
如第一张表和第二张表有相同的id列,第二张和第三张有相同的class列
语句如下
select * from 表一名 left join 表二名 on 表一名.id =表二名.id 表二名 left join 表三名 on 表二名.class=表三名.class where 条件

2.小知识点

"""
1.在scrapy中,因为xpath()  返回的是一个Selector
 xpath().extract()  将查找的数据转成列表,若不存在,会执行extract('默认值')默认值
 xpath().extract_first()  转换为列表并取第一个值

2.isdigit()
Python isdigit() 方法检测字符串是否只由数字组成。
语法
isdigit()方法语法:
str.isdigit()
如果字符串只包含数字则返回 True 否则返回 False

3. math.ceil() 向上取整,如math.ceil(3.1)  4
 math.floor() 向下取整,如math.floor(3.1)  3
round() 四舍五入
math.pi 圆周率
 math.pow(a, b) a的b次方 返回小数 等价于 a ** b  返回整数


4.scrapy主动关闭爬虫
# 方式1
self.crawler.engine.close_spider(self, 'cookie失效关闭爬虫')
# 方式2
raise CloseSpider('cookie失效关闭爬虫')

4.scrapy定时爬虫

import time
import os

while True:
    os.system("scrapy crawl News")
    time.sleep(86400)  #每隔一天运行一次 24*60*60=86400s
"""

3.主动退出爬虫的两种形式

在知识点二中

4.如何实现定时爬虫

自定义一个脚本,放在爬虫项目下,内容如下,最简单的定时爬虫

import time
import os

while True:
    os.system("scrapy crawl News")
    time.sleep(86400)  #每隔一天运行一次 24*60*60=86400s

猜你喜欢

转载自blog.csdn.net/qq_41150890/article/details/99684306