【问题标题】:extract the title of the url using scrapy,python使用scrapy,python提取url的标题
【发布时间】:2017-02-07 19:10:06
【问题描述】:

我是 Python 的 Scrapy 新手。我需要提取网址的标题而不是上下文。下面的代码将内容连同标题一起提取出来。请在上面提供帮助

提前谢谢你。

class BlogSpider(scrapy.Spider):
         name = 'bg'
         start_urls = ['https://blog.scrapinghub.com', 'https://scrapinghub.com/']

     def parse(self, response):
        for title in response.css('h2.entry-title'):
            yield {'title': title.css('a ::text').extract_first()}

        page = response.url.split("/")[-2]
        filename = 'urltitle-%s.html' %page
        with open(filename,'wb') as f:
           f.write(response.body)

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    不确定我是否正确理解“标题”是什么意思,但如果您需要提取标签atitle 属性,您可以使用适当的选择器title.css('a::attr(title)') 提取它

    【讨论】:

    • 你是对的,非常感谢 mizhgun。我还尝试使用 beautifulsoup 进行提取,我希望分享它 soup = BeautifulSoup(response.text,'lxml') yield{ "title" : soup.title.string }
    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多