爬虫实现二级链接页面信息爬取
一.scrapy环境搭建,参考我的博客–>爬虫框架虚拟环境搭建
二.scrapy设置配置
1.设置用户代理
进入页面并刷新,进入开发者模式,点击选中一个网页,在Network-Headers中找到USER_AGENT,并复制就可以了.
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
2.是否遵守爬虫协议改为否(原因你懂的)
ROBOTSTXT_OBEY = False
3.一次允许的最大请求数
# Configure maximum concurrent requests performed by Scrapy (default: 16) CONCURRENT_REQUESTS = 2
4.设置下载延迟时间,因而使得爬虫更像是人的行为,避免IP被屏蔽
DOWNLOAD_DELAY = 3
5.设置下载中间键
DOWNLOADER_MIDDLEWARES = { 'xymtest.middlewares.XymtestDownloaderMiddleware': 543, }
6.设置管道
ITEM_PIPELINES = { 'xymtest.pipelines.XymtestPipeline': 300, }
7.取消最后几行的注释
HTTPCACHE_ENABLED = True HTTPCACHE_EXPIRATION_SECS = 0 HTTPCACHE_DIR = 'httpcache' HTTPCACHE_IGNORE_HTTP_CODES = [] HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
三.开始编写爬虫代码
1.设置要爬取得items
# define the fields for your item here like: title = scrapy.Field() content = scrapy.Field()
2.在spider下创建一个.py文件,编写爬虫代码.
class testInformation(scrapy.Spider): name = 'test' #域名后面跟的一串数字不要跟上来 allowed_domains = ['blog.test.net'] #the format of different page's address is https://blog.test.net/u42/article/list/ follows with a number,https://blog.test.net/u042/article/list/1 means the first page, https://blog.test.net/u014229742/article/list/2 is the second.so we can use a the same part plus a number,but we can not add a int type with a string,so we change the number to str
start_urls = ['https://blog.test.net/u42/article/list/' + str(x) for x in range(1, 4)]
#函数 def parse(self, response):
#get the xpath of the title: #the first title xpath is://*[@id="mainBox"]/main/div[2]/div[1]/h4/a #the seconde title xpath is://*[@id="mainBox"]/main/div[2]/div[2]/h4/a #the same part is://*[@id="mainBox"]/main/div[2],and from the next div everything is different.div[1] means the first title's xpath.div[2] means the second title's xpath. #so if when want to get all the xpath,we can use://*[@id="mainBox"]/main/div[2]/div #获取到所有标题的xpath li_list = response.xpath('//*[@id="mainBox"]/main/div[2]/div')
#we have to get all the title,so there must has a recycle,xq means one of the for xq in li_list: item = XymtestItem() #获取到标题内容//*[@id="mainBox"]/main/div[2]/div[1]/h4/a/text() #//*[@id="mainBox"]/main/div[2]/div[2]/h4/a/text() #获取到的标题去掉li_list中的公共部分 item_list = xq.xpath('h4/a/text()').extract() #因为获取到的item_list有空的内容,如果直接extract()[0],会报错,故先判断长度,长度不为空,开始取标题 if len(item_list) > 0: #strip()函数可以去除空格 item['title'] = item_list[1].strip() #获取到每个标题的href内容 url = xq.xpath('h4/a/@href').extract()[0] #Request(url, meta={'item': item}, callback = self.parse_detail)方法实现二层链接函数的调取 yield Request(url, meta={'item': item}, callback = self.parse_detail)
def parse_detail(self, response):
item = response.meta['item'] #获取到二层链接中要爬取的页面的xpath item['content'] = response.xpath('//*[@id="mainBox"]/main/div[1]/div[2]/div/div/span/text()').extract()[0]
yield item 好了,以上代码基本实现了一个二层链接的爬取,接下来要做的事将爬取到的数据存储到数据库中供我们使用.想知道更多,继续关注小姐姐!
相关文章
- Python使用tkinter组件Label显示简单数学公式
- 内网渗透之DCOM横向移动
- 以目标为导向的语义交流的共同语言——一个课程学习框架
- python爬虫前奏【成信笔记】
- HTML 5 File API:文件拖放上传功能
- 教你快速创建 Python 虚拟环境
- pyenv 实现Python多版本自由切换
- 用 Python 对 Excel文件进行批量操作
- Python - 接入钉钉机器人
- Python - 抓取 iphone13 pro 线下店供货信息并发送到钉钉机器人,最后设置为定时任务
- crontab - 解决 mac 下通过 crontab 设置了 Python 脚本的定时任务却无法运行
- [源码解析] PyTorch分布式(5) ------ DistributedDataParallel 总述&如何使用
- Python科普系列——类与方法(上篇)
- SAP对STO的交货单执行PGI,报错 -Fld selectn for mvmt type 643 acct 400020 differs
- Spring Boot 实现通用 Auth 认证的 4 种方式
- 盘点4种使用Python批量合并同一文件夹内所有子文件夹下的Excel文件内所有Sheet数据
- OushuDB 学习经验分享(三):技术特点
- Java和Python思维方式的不同之处
- Python中日志记录新技能
- 奥比中光Gemini OpenCV—Python使用