lzb1001 发表于 2022-5-8 21:14:45

response.xpath()到底是小括号还是尖括号?

本帖最后由 lzb1001 于 2022-5-8 21:16 编辑



看小甲鱼在视频讲解里的所演示,看起来好像是尖括号<>

比如第一行,看起来是:

>>> response.xpath<'//title'>

而教材里则是:

>>> response.xpath('//title')

到底哪个是对的???

wp231957 发表于 2022-5-8 21:17:21

当然是小括号了
尖括号是html里的东西

wp231957 发表于 2022-5-8 21:18:33

你自己去shell下敲一敲就知道了,只是看起来像而已

Twilight6 发表于 2022-5-8 21:20:04



第二个是对的,圆括号

甲鱼哥的字体原因看上去像尖括号

isdkz 发表于 2022-5-8 21:20:40

方法调用肯定用的是小括号呀

hrpzcf 发表于 2022-5-8 21:20:58

是小括号。python语法没有尖括号,只有大于小于号,看起来像尖括号是字体的问题。

lzb1001 发表于 2022-5-8 21:39:12

dmoz案例没法跟着操作,学起来真是不方便,希望小甲鱼能重新选择案例并录入新视频

lzb1001 发表于 2022-5-8 21:42:43

wp231957 发表于 2022-5-8 21:18
你自己去shell下敲一敲就知道了,只是看起来像而已

不是不想亲手敲,而是:dmoz案例没法跟着操作,学起来真是不方便

wp231957 发表于 2022-5-8 21:44:27

lzb1001 发表于 2022-5-8 21:42
不是不想亲手敲,而是:dmoz案例没法跟着操作,学起来真是不方便

你就随便敲个小括号再随便敲个尖括号不就完了

lzb1001 发表于 2022-5-8 22:05:22

wp231957 发表于 2022-5-8 21:44
你就随便敲个小括号再随便敲个尖括号不就完了

都是返回错误,虽然错误原因不同,还是有案例有正确返回感觉学起来或敲代码有动力,尤其看到亲手敲代码能有相同正确结果返回,学起来更有获得感啊

>>> response.xpath<'//title'>
File "<stdin>", line 1
    response.xpath<'//title'>
                            ^
SyntaxError: invalid syntax

>>> response.xpath('//title')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'response' is not defined

lzb1001 发表于 2022-5-8 22:10:06

isdkz 发表于 2022-5-8 21:20
方法调用肯定用的是小括号呀

https://fishc.com.cn/thread-213082-1-1.html
--------------------------------------
D:\work\tutorial>scrapy shell 'http://dmoztools.net/Computers/Programming/Languages/Python/Books/'

2022-05-07 23:56:37 INFO: Scrapy 2.6.1 started (bot: tutorial)
2022-05-07 23:56:37 INFO: Versions: lxml 4.8.0.0, libxml2 2.9.12, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 22.4.0, Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) , pyOpenSSL 22.0.0 (OpenSSL 3.0.3 3 May 2022), cryptography 37.0.2, Platform Windows-10-10.0.19041-SP0
2022-05-07 23:56:37 INFO: Overridden settings:
{'BOT_NAME': 'tutorial',
'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter',
'LOGSTATS_INTERVAL': 0,
'NEWSPIDER_MODULE': 'tutorial.spiders',
'ROBOTSTXT_OBEY': True,
'SPIDER_MODULES': ['tutorial.spiders']}
2022-05-07 23:56:37 DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2022-05-07 23:56:37 INFO: Telnet Password: b1f68aed69443fa2
2022-05-07 23:56:37 INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole']
2022-05-07 23:56:37 INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2022-05-07 23:56:37 INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2022-05-07 23:56:37 INFO: Enabled item pipelines:
[]
2022-05-07 23:56:37 INFO: Telnet console listening on 127.0.0.1:6023
2022-05-07 23:56:37 INFO: Spider opened
2022-05-07 23:56:38 ERROR: Error downloading <GET http://'http/robots.txt>: invalid hostname: 'http
Traceback (most recent call last):
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\core\downloader\middleware.py", line 49, in process_request
    return (yield download_func(request=request, spider=spider))
ValueError: invalid hostname: 'http
Traceback (most recent call last):
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\Scripts\scrapy.exe\__main__.py", line 7, in <module>
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\cmdline.py", line 145, in execute
    _run_print_help(parser, _run_command, cmd, args, opts)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\cmdline.py", line 100, in _run_print_help
    func(*a, **kw)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\cmdline.py", line 153, in _run_command
    cmd.run(args, opts)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\commands\shell.py", line 74, in run
    shell.start(url=url, redirect=not opts.no_redirect)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\shell.py", line 43, in start
    self.fetch(url, spider, redirect=redirect)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\shell.py", line 111, in fetch
    reactor, self._schedule, request, spider)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\twisted\internet\threads.py", line 120, in blockingCallFromThread
    result.raiseException()
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\twisted\python\failure.py", line 500, in raiseException
    raise self.value.with_traceback(self.tb)
ValueError: invalid hostname: 'http

D:\work\tutorial>

按小甲鱼视频中讲解,运行最后将返回>>>,但看上面好像不是,最后又返回D:\work\tutorial>,而且还有一大堆错误,是哪里有问题哦?

我将命令行中的网址的单引号换成双引号:---是不是必须用双引号,不能用单引号啊???

D:\work\tutorial>scrapy shell "http://dmoztools.net/Computers/Programming/Languages/Python/Books/"

2022-05-08 00:00:37 INFO: Scrapy 2.6.1 started (bot: tutorial)
2022-05-08 00:00:37 INFO: Versions: lxml 4.8.0.0, libxml2 2.9.12, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 22.4.0, Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) , pyOpenSSL 22.0.0 (OpenSSL 3.0.3 3 May 2022), cryptography 37.0.2, Platform Windows-10-10.0.19041-SP0
2022-05-08 00:00:37 INFO: Overridden settings:
{'BOT_NAME': 'tutorial',
'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter',
'LOGSTATS_INTERVAL': 0,
'NEWSPIDER_MODULE': 'tutorial.spiders',
'ROBOTSTXT_OBEY': True,
'SPIDER_MODULES': ['tutorial.spiders']}
2022-05-08 00:00:37 DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2022-05-08 00:00:37 INFO: Telnet Password: bb2332e9ee9c5698
2022-05-08 00:00:37 INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole']
2022-05-08 00:00:38 INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2022-05-08 00:00:38 INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2022-05-08 00:00:38 INFO: Enabled item pipelines:
[]
2022-05-08 00:00:38 INFO: Telnet console listening on 127.0.0.1:6023
2022-05-08 00:00:38 INFO: Spider opened
2022-05-08 00:00:38 DEBUG: Redirecting (301) to <GET https://dmoztools.net/robots.txt> from <GET http://dmoztools.net/robots.txt>
2022-05-08 00:00:38 WARNING: C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\scrapy\core\engine.py:276: ScrapyDeprecationWarning: Passing a 'spider' argument to ExecutionEngine.download is deprecated
return self.download(result, spider) if isinstance(result, Request) else result

2022-05-08 00:00:39 DEBUG: Crawled (200) <GET https://dmoztools.net/robots.txt> (referer: None)
2022-05-08 00:00:40 DEBUG: Redirecting (301) to <GET https://dmoztools.net/Computers/Programming/Languages/Python/Books/> from <GET http://dmoztools.net/Computers/Programming/Languages/Python/Books/>
2022-05-08 00:00:40 DEBUG: Crawled (404) <GET https://dmoztools.net/Computers/Programming/Languages/Python/Books/> (referer: None)
2022-05-08 00:00:41 DEBUG: Using selector: SelectSelector
Available Scrapy objects:
   scrapy   scrapy module (contains scrapy.Request, scrapy.Selector, etc)
   crawler    <scrapy.crawler.Crawler object at 0x000001A105E874C8>
   item       {}
   request    <GET http://dmoztools.net/Computers/Programming/Languages/Python/Books/>
   response   <404 https://dmoztools.net/Computers/Programming/Languages/Python/Books/>
   settings   <scrapy.settings.Settings object at 0x000001A105E83F08>
   spider   <DmoztoolsSpider 'dmoztools' at 0x1a10656c308>
Useful shortcuts:
   fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
   fetch(req)                  Fetch a scrapy.Request and update local objects
   shelp()         Shell help (print this help)
   view(response)    View response in a browser
2022-05-08 00:00:41 DEBUG: Using selector: SelectSelector
In :

显示结果怎么有删除线?我附上返回的结果图吧:

wp231957 发表于 2022-5-8 22:10:45

lzb1001 发表于 2022-5-8 22:05
都是返回错误,虽然错误原因不同,还是有案例有正确返回感觉学起来或敲代码有动力,尤其看到亲手敲代码能 ...

我只是让你敲括号,看看括号长啥样,如此而已

lzb1001 发表于 2022-5-8 22:12:33

wp231957 发表于 2022-5-8 22:10
我只是让你敲括号,看看括号长啥样,如此而已

哦,我是看视频觉得很像尖括号,觉得疑惑所以提出来问下
页: [1]
查看完整版本: response.xpath()到底是小括号还是尖括号?