创建一个程序demo.py,再用代码生成一个demo_new.py,使其每一行的后面注释上行号(#1),并且所有行的#垂直对齐

首先在桌面创建一个文本文档,并将文本文档——demo.py

之后再在python中输入以下代码

filename = (‘C:/Users/YKL/Desktop/demo.py’)
with open(filename, ‘r’) as fp:
lines = fp.readlines()
maxLength = len(max(lines, key=len))

lines = [line.rstrip().ljust(maxLength)+’#’+str(index)+’\n’
for index, line in enumerate(lines)]
with open(filename[:-3]+’_new.py’, ‘w’) as fp:
fp.writelines(lines)

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
文件: import scrapy from demo1.items import Demo1Item import urllib from scrapy import log # BOSS直聘网站爬虫职位 class DemoSpider(scrapy.Spider): # 爬虫名, 启动爬虫时需要的参数*必填 name = &#39;demo&#39; # 爬取域范围,允许爬虫在这个域名下进爬取(可选) allowed_domains = [&#39;zhipin.com&#39;] # 爬虫需要的url start_urls = [&#39;https://www.zhipin.com/c101280600/h_101280600/?query=测试&#39;] def parse(self, response): node_list = response.xpath("//div[@class=&#39;job-primary&#39;]") # 用来存储所有的item字段 # items = [] for node in node_list: item = Demo1Item() # extract() 将xpath对象转换为Unicode字符串 href = node.xpath("./div[@class=&#39;info-primary&#39;]//a/@href").extract() job_title = node.xpath("./div[@class=&#39;info-primary&#39;]//a/div[@class=&#39;job-title&#39;]/text()").extract() salary = node.xpath("./div[@class=&#39;info-primary&#39;]//a/span/text()").extract() working_place = node.xpath("./div[@class=&#39;info-primary&#39;]/p/text()").extract() company_name = node.xpath("./div[@class=&#39;info-company&#39;]//a/text()").extract() item[&#39;href&#39;] = href[0] item[&#39;job_title&#39;] = job_title[0] item[&#39;sa 报错: C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\python.exe "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\pydevconsole.py" --mode=client --port=55825 import sys; print(&#39;Python %s on %s&#39; % (sys.version, sys.platform)) sys.path.extend([&#39;C:\\Users\\xieqianyun\\demo1&#39;, &#39;C:/Users/xieqianyun/demo1&#39;]) Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] Type &#39;copyright&#39;, &#39;credits&#39; or &#39;license&#39; for more information IPython 7.10.0 -- An enhanced Interactive Python. Type &#39;?&#39; for help. PyDev console: using IPython 7.10.0 Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 runfile(&#39;C:/Users/xieqianyun/demo1/demo1/begin.py&#39;, wdir=&#39;C:/Users/xieqianyun/demo1/demo1&#39;) Traceback (most recent call last): File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-fc5979762143>", line 1, in <module> runfile(&#39;C:/Users/xieqianyun/demo1/demo1/begin.py&#39;, wdir=&#39;C:/Users/xieqianyun/demo1/demo1&#39;) File "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, &#39;exec&#39;), glob, loc) File "C:/Users/xieqianyun/demo1/demo1/begin.py", line 3, in <module> cmdline.execute(&#39;scrapy crawl demo&#39;.split()) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\cmdline.py", line 145, in execute cmd.crawler_process = CrawlerProcess(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 267, in __init__ super(CrawlerProcess, self).__init__(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 145, in __init__ self.spider_loader = _get_spider_loader(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 347, in _get_spider_loader return loader_cls.from_settings(settings.frozencopy()) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 61, in from_settings return cls(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 25, in __init__ self._load_all_spiders() File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 47, in _load_all_spiders for module in walk_modules(name): File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\utils\misc.py", line 73, in walk_modules submod = import_module(fullpath) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\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 "C:\Users\xieqianyun\demo1\demo1\spiders\demo.py", line 4, in <module> from scrapy import log ImportError: cannot import name &#39;log&#39;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值