欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

爬取Tapd的缺陷记录

程序员文章站 2022-07-08 17:15:59
# -*- coding: utf-8 -*-import scrapyfrom urllib.parse import urlparse, parse_qs,parse_qslclass TapdSpider(scrapy.Spider): name = 'tapd' allowed_domains = ['tapd.cn'] url = 'https://www.tapd.cn/xxxxxxxx/bugtrace/bugreports/my_view&page={...
# -*- coding: utf-8 -*-
import scrapy
from urllib.parse import urlparse, parse_qs,parse_qsl

class TapdSpider(scrapy.Spider):
    name = 'tapd'
    allowed_domains = ['tapd.cn']
    url = 'https://www.tapd.cn/xxxxxxxx/bugtrace/bugreports/my_view&page={}'
    page = 1
    start_urls = ['https://www.tapd.cn/xxxxxxxx/bugtrace/bugreports/my_view&page={}'.format(str(page))]

    def parse(self, response):
        try:
            tr_list = response.xpath("//table[@id='bug_list_content']//tr[@class='rowNOTdone']")
            #写列表的时候我们只要定位到想要的父级,即存在多个同标签的位置,方便我们接下来去循环遍历
            for tr in tr_list:
                item = {}
                # 在当前的节点下方开始找元素,用./
                # 遇到想用的属性对应的属性值的时候,可以定位到该标签后,@属性的名称获取相应的值
                # extract()返回的是一个列表,extract_first()返回的是字符串,列表中的第一个字符串
                item["title"] = tr.xpath("./td[3]//span[2]/a/@title").extract_first()
                item["version"] = tr.xpath("./td[4]//@data-editable-value").extract_first()
                item["Severity"] = tr.xpath("./td[5]//@data-editable-value").extract_first()
                item["priority"] = tr.xpath("./td[6]//@data-editable-value").extract_first()
                item["statue"] = tr.xpath("./td[7]//@title").extract_first()
                item["Handler"] = tr.xpath("./td[8]//@data-editable-value").extract_first()
                item["Founder"] = tr.xpath("./td[9]//@title").extract_first()
                item["Creattime"] = tr.xpath('string(./td[10][contains(.,text())])').extract_first().strip()
                # response.xpath('string(元素的定位)[contains()]')
                # string可以获取同个父级多个子级的内容
                # contains()模糊查找当前的文本内容
                # strip()可以去除首尾的空格 举例strip('0'),可以去除首尾的0 仅针对字符串使用
                # https://blog.csdn.net/zhouxuan623/article/details/43935039/
                # xpath中starts-with、contains和text()三者区别
                item["href"] = tr.xpath("./td[3]//span[2]//@href").extract_first()
                Pending_url = item["href"]
                bug_id = parse_qs(urlparse(Pending_url).query)["bug_id"][0]
                # 我需要获取url地址中的部分项,重新拼接成新的url地址
                # https://blog.csdn.net/weixin_42902669/article/details/88907704
                detail_url = 'https://www.tapd.cn/xxxxxxxx/bugtrace/bugs/general_view/{}'.format(bug_id)

                yield scrapy.Request(
                        detail_url,
                        callback=self.parse_detial,
                        meta={"key": item}
                )
            self.page +=1
            next_url = self.url.format(str(self.page))
            print(next_url)
            yield scrapy.Request(
                next_url,
                callback=self.parse
            )
        except TypeError:
            print("爬取结束")

    def parse_detial(self, response):
        item = response.meta["key"]
        content = response.xpath("//div[@id='description_div']").xpath('string(.)').extract()[0]
        # 定位到要获取文本内容的父级,通过父级获取该父级下的子集所有文本内容,extract()[0] = extract_first()
        item["data"] = content
        yield item
本文地址:https://blog.csdn.net/Python_BT/article/details/108246268