scrapy 我的乱码不起作用,但一切似乎都很好

vof42yt1  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(195)

你好,我试着从www.soccerstats.com.My代码中抓取一些信息,下面的代码不是从该网站抓取任何信息。输出是:

{'BOT_NAME': 'superlig',
 'NEWSPIDER_MODULE': 'superlig.spiders',
 'REQUEST_FINGERPRINTER_IMPLEMENTATION': '2.7',
 'SPIDER_MODULES': ['superlig.spiders'],
 'TWISTED_REACTOR': 'twisted.internet.asyncioreactor.AsyncioSelectorReactor',
 'USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
               '(KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}
2022-11-01 19:55:49 [asyncio] DEBUG: Using selector: SelectSelector
2022-11-01 19:55:49 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.asyncioreactor.AsyncioSelectorReactor
2022-11-01 19:55:49 [scrapy.utils.log] DEBUG: Using asyncio event loop: asyncio.windows_events._WindowsSelectorEventLoop
2022-11-01 19:55:49 [scrapy.extensions.telnet] INFO: Telnet Password: d7ca26fc46152e7d
2022-11-01 19:55:49 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.logstats.LogStats']
2022-11-01 19:55:49 [scrapy.middleware] INFO: Enabled downloader middlewares:
['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-11-01 19:55:49 [scrapy.middleware] 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-11-01 19:55:49 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2022-11-01 19:55:49 [scrapy.core.engine] INFO: Spider opened
2022-11-01 19:55:49 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2022-11-01 19:55:49 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2022-11-01 19:55:50 [filelock] DEBUG: Attempting to acquire lock 1903475372576 on C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2022-11-01 19:55:50 [filelock] DEBUG: Lock 1903475372576 acquired on C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2022-11-01 19:55:50 [filelock] DEBUG: Attempting to release lock 1903475372576 on C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2022-11-01 19:55:50 [filelock] DEBUG: Lock 1903475372576 released on C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\tldextract\.suffix_cache/publicsuffix.org-tlds\de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2022-11-01 19:55:50 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.soccerstats.com/latest.asp?league=turkey> (referer: None)
2022-11-01 19:55:50 [scrapy.core.engine] INFO: Closing spider (finished)
2022-11-01 19:55:50 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 320,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 43248,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'elapsed_time_seconds': 1.018966,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2022, 11, 1, 16, 55, 50, 519778),
 'httpcompression/response_bytes': 279559,
 'httpcompression/response_count': 1,
 'log_count/DEBUG': 8,
 'log_count/INFO': 10,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2022, 11, 1, 16, 55, 49, 500812)}
2022-11-01 19:55:50 [scrapy.core.engine] INFO: Spider closed (finished)

我在www.example.com上设置了一个user_agentsettings.py,并在代码中添加了time.sleep。而且我的代码在调试时也得到了正确的输出。我的代码如下:

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
import time

class SoccerstatsSpider(CrawlSpider):
    name = 'soccerstats'
    allowed_domains = ['soccerstats.com']
    start_urls =['https://www.soccerstats.com/latest.asp?league=turkey']

    #user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'

    rules = (
        Rule(LinkExtractor(restrict_xpaths="//div[@style='width:644px;margin-left:10px;padding-left:4px;margin-right:0px;margin-top:3px;float:left;']/table[3]/tbody/tr[@class='odd']/td[2]/a"), callback='parse_item', follow=True),
    )
    #def set_user_agent(self,request):
        #request.headers['User-Agent']=self.user_agent

    time.sleep(0.5)
    def parse_item(self, response):
        yield{
            'Team' : response.xpath(".//div[@id='content']/div[1]/div[2]/font/text()").get(),
        }

请帮助我并解释我做错了什么。
我尝试添加一个用户代理和time.sleep()

yptwkmov

yptwkmov1#

问题出在LinkExtractor xpath选择器上。虽然xpath突出显示了网站上的元素。但当我执行scrapy shell命令来获取该选择器的结果元素时,我什么也得不到。
你可以把选择器简化成下面这样的东西,它对我很有效,从网站上抓取了一些条目。

...

rules = (
    Rule(LinkExtractor(restrict_xpaths='//td/a[@itemprop="url"]'), callback='parse_item', follow=True),
)

...

这应该对你有用。如果有用就告诉我。

相关问题