python-3.x - AttributeError:“字节”对象没有属性“获取”
问题描述
我正在尝试从https://www.gizbot.com/mobile-brands-in-india/中提取所有品牌名称。下面是 mobiles_spiders.py 文件的代码
class MobilesSpider(scrapy.Spider):
name = "mobiles"
def start_requests(self):
urls = [
'https://www.gizbot.com/mobile-brands-in-india/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = 'mobiles-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.xpath(str.encode('.//div[has-class("all-brands-block-desc-brand")]/text()').get()))
self.log('Saved file %s' % filename)
但是代码给了我错误,因为 AttributeError: 'bytes' 对象没有属性 'get' 我需要关于我需要使用什么函数而不是 get() 来提取包含品牌名称的所有 div 元素的建议。任何帮助表示赞赏。
解决方案
它可能会帮助你。
import scrapy
class MobilesSpider(scrapy.Spider):
name = "mobiles"
def start_requests(self):
urls = [
'https://www.gizbot.com/mobile-brands-in-india/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = 'mobiles-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.xpath('.//div[has-class("all-brands-block-desc-brand")]/text()').get().encode('utf-8'))
self.log('Saved file %s' % filename)
推荐阅读
- regex - pcregrep 或 grep:使用前瞻搜索不起作用
- python - 在尝试微调 CNN 模型时,我如何决定一个合适的头部网络来附加到模型上?
- react-native - 如何在本机反应中清楚地删除库?
- php - Apache 正在运行线程 MPM,但您的 PHP 模块未编译为线程安全的。您需要重新编译 PHP。AH00013:预配置失败
- android - 如何在 Android 上访问语音通话和 VoIP 通话的输入和输出音频流?
- php - mouseover 显示原始 html 数据并触发无限时间
- sql - 在我的情况下,如何使用 SQL Server 将行转换为列?
- javascript - 如何根据事件以不同颜色突出显示日历中的日期?
- swift - 如何编码/解码 [CKRecordZone.ID: CKServerChangeToken]?
- scala - 带有过滤器列的 Scala