您当前的位置: 首页 >  Python

Python爬虫:Scrapy链接解析器LinkExtractor返回Link对象

彭世瑜 发布时间:2018-06-01 15:46:56 ,浏览量:3

LinkExtractor
from scrapy.linkextractors import LinkExtractor

Link
from scrapy.link import Link

Link四个属性

url text fragment nofollow

如果需要解析出文本,需要在 LinkExtractor 的参数中添加参数:attrs

link_extractor = LinkExtractor(attrs=('href','text'))

links = link_extractor.extract_links(response)

使用示例

import scrapy

from scrapy.linkextractors import LinkExtractor


class DemoSpider(scrapy.Spider):
    name = 'spider'

    
    start_urls = [
        "https://book.douban.com/"
    ]

    def parse(self, response):
        # 参数是正则表达式
        link_extractor = LinkExtractor(allow="https://www.tianyancha.com/brand/b.*")

        links = link_extractor.extract_links(response)

        for link in links:
            print(link.text, link.url)


if __name__ == '__main__':
    cmdline.execute("scrapy crawl spider".split())

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 3浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0474s