gpt4 book ai didi

python - 列表理解中的字符串格式

转载 作者:行者123 更新时间:2023-11-28 18:17:18 25 4
gpt4 key购买 nike

<分区>

我正在开发网络抓取工具,但在列表推导中使用字符串占位符时,我偶然发现了这种奇怪的行为(这是我在 Pycharm 中的代码片段):

# -*- coding: utf-8 -*-
from arms_transfers.items import ArmsTransferItem
import itertools
import pycountry
import scrapy
import urllib3


class UnrocaSpider(scrapy.Spider):
name = 'unroca'
allowed_domains = ['unroca.org']

country_names = [country.official_name if hasattr(country, 'official_name')
else country.name for country in list(pycountry.countries)]
country_names = [name.lower().replace(' ', '-') for name in country_names]

base_url = 'https://www.unroca.org/{}/report/{}/'
url_param_tuples = list(itertools.product(country_names, range(2010, 2017)))
start_urls = [base_url.format(param_tuple[0], param_tuple[1]) for param_tuple in url_param_tuples]

这里是错误:

Traceback (most recent call last):
File "anaconda3/envs/scraper/bin/scrapy", line 11, in <module>
sys.exit(execute())
File "anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/cmdline.py", line 148, in execute
cmd.crawler_process = CrawlerProcess(settings)
File "anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/crawler.py", line 243, in __init__
super(CrawlerProcess, self).__init__(settings)
File "anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/crawler.py", line 134, in __init__
self.spider_loader = _get_spider_loader(settings)
File "/anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/crawler.py", line 330, in _get_spider_loader
return loader_cls.from_settings(settings.frozencopy())
File "anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/spiderloader.py", line 61, in from_settings
return cls(settings)
File "anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/spiderloader.py", line 25, in __init__
self._load_all_spiders()
File "anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/spiderloader.py", line 47, in _load_all_spiders
for module in walk_modules(name):
File "anaconda3/envs/scraper/lib/python3.6/site-packages/scrapy/utils/misc.py", line 71, in walk_modules
submod = import_module(fullpath)
File "anaconda3/envs/scraper/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "Programming/my_projects/web-scrapers/arms_transfers/arms_transfers/spiders/unroca.py", line 9, in <module>
class UnrocaSpider(scrapy.Spider):
File "Programming/my_projects/web-scrapers/arms_transfers/arms_transfers/spiders/unroca.py", line 19, in UnrocaSpider
start_urls = [base_url.format(param_tuple[0], param_tuple[1]) for param_tuple in url_param_tuples]
File "Programming/my_projects/web-scrapers/arms_transfers/arms_transfers/spiders/unroca.py", line 19, in <listcomp>
start_urls = [base_url.format(param_tuple[0], param_tuple[1]) for param_tuple in url_param_tuples]
NameError: name 'base_url' is not defined

奇怪的是,当我在 Jupyter notebook 中运行它时:

import pycountry
import itertools

country_names = [country.official_name if hasattr(country, 'official_name')
else country.name for country in list(pycountry.countries)]
country_names = [name.lower().replace(' ', '-') for name in country_names]

base_url = 'https://www.unroca.org/{}/report/{}/'
url_param_tuples = list(itertools.product(country_names, range(2010, 2017)))
start_urls = [base_url.format(param_tuple[0], param_tuple[1]) for param_tuple in url_param_tuples]

它的工作方式与我在 Pycharm 项目中的预期一样:

 ['https://www.unroca.org/aruba/report/2010/',
'https://www.unroca.org/aruba/report/2011/',
'https://www.unroca.org/aruba/report/2012/',
'https://www.unroca.org/aruba/report/2013/',
'https://www.unroca.org/aruba/report/2014/',
'https://www.unroca.org/aruba/report/2015/',
'https://www.unroca.org/aruba/report/2016/',
'https://www.unroca.org/islamic-republic-of-afghanistan/report/2010/',
'https://www.unroca.org/islamic-republic-of-afghanistan/report/2011/',
'https://www.unroca.org/islamic-republic-of-afghanistan/report/2012/',
'https://www.unroca.org/islamic-republic-of-afghanistan/report/2013/',...]

Pycharm 项目和 Jupyter notebook 使用相同的 conda 环境和 Python 3.6.3 解释器。任何人都可以深入了解导致行为差异的原因吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com