刮削中的行进路径

2024-04-25 14:40:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一些网页抓取和我有一些问题与Python/Scrapy。 我已经隔离了我想访问的链接,但不知道如何去那里获取更多的数据。到目前为止,我得到的是这样的东西:

def parse_site(self, response):
    sel = Selector(response)
    sites = sel.xpath('//a')

    # This part works and is responsible for getting only the links I want
    sites = [site for site in sites if "." in str(site.xpath('text()').extract())]
    items = []
    for site in sites:
        item = DomainManagerItem()

        dName = str(site.xpath('text()').extract())[3:-2]
        item['domainName'] = dName

此时,我想将下一页的信息存储到项目的第二个字段中。我正在努力做到以下几点。我把基址存储在BASE_ADDRESS中,我用newPath = str(site.xpath("@href").extract())拉取地址的第二部分,如果我print输出BASE_ADDRESS + newPath,它就是我要找的。我现在所做的是尝试通过使用另一个模块从下一页获得一些东西。然而,我不能让它工作。看起来像这样。你知道吗

        item['totalUsers'] = self.parse_client(ResponseObj)

我不知道如何得到正确的回应对象,并尝试了许多不同的事情。但还是不能让它工作。我想我可以解析下一个文件并从中获取totalUsers,只需将它发送到下一个模块。你知道吗

谢谢你的帮助。你知道吗


Tags: textinselfforbaseparseresponsesite
1条回答
网友
1楼 · 发布于 2024-04-25 14:40:51

结果是我对Python的一些东西不太了解,并试图掌握scrapy。这是我使用的有效解决方案。。你知道吗

def parse_site(self, response):
    global BASE_WEBSITE
    sel = Selector(response)
    sites = sel.xpath('//a') # xpath for the sites
    # To get the desc. of the <a> tag use sel.xpath('//a/text()').extract()
    # Get all the websites that would lead to clients.
    sites = [site for site in sites if "." in str(site.xpath('text()').extract())]
   # items = []
    for site in sites:
        item = DomainManagerItem()

        # Get the Description and trim it
        dName = str(site.xpath('text()').extract())[3:-2]

        # Get the Path and trim it
        newPath = "https://" +  BASE_WEBSITE +  (str(site.xpath("@href").extract())[3:-2])

        item['domainName'] = dName
        yield Request(url = newPath, callback = self.parse_client, meta = {'item':item})


def parse_client(self, response):
    sel = Selector(response)
    ite = response.meta['item']
    site = sel.xpath('//td')
    ite['totalUsers'] = str(site[8].xpath('text()').extract())[3:-2]

    return ite

相关问题 更多 >