我是 Scrapy、Python 的新手。我需要提取网址的标题而不是上下文。下面的代码提取内容和标题。请对上述内容提供帮助
提前谢谢您。
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)
最佳答案
不确定我是否正确理解“标题”的含义,但如果您需要提取标签 a
的 title
属性,您可以使用适当的选择器提取它title.css('a::attr(title)')
关于python - 使用scrapy、python提取url的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42080992/