【问题标题】:readlines() error with for-loop in pythonpython中for循环的readlines()错误
【发布时间】:2016-12-12 23:20:41
【问题描述】:

这个错误很难描述,因为我无法弄清楚循环是如何影响readline()readlines() 方法的。当我尝试使用前者时,我得到了这些意外的 Traceback 错误。当我尝试后者时,我的代码运行并且没有任何反应。我已经确定该错误位于前八行。 Topics.txt 文件的前几行已发布。

Code

import requests
from html.parser import HTMLParser
from bs4 import BeautifulSoup

Url = "https://ritetag.com/best-hashtags-for/"
Topicfilename = "Topics.txt"
Topicfile = open(Topicfilename, 'r')
Line = Topicfile.readlines()
Linenumber = 0
for Line in Topicfile:
    Linenumber += 1
    print("Reading line", Linenumber)

    Topic = Line
    Newtopic = Topic.strip("\n").replace(' ', '').replace(',', '')
    print(Newtopic)
    Link = Url.join(Newtopic)
    print(Link)
    Sourcecode = requests.get(Link)

当我在此处运行此位时,它会打印以该行的第一个字符开头的 URL。例如,它会打印为 2https://ritetag.com/best-hashtags-for/4https://ritetag。 com/best-hashtags-for/Hhttps://ritetag.com/best-hashtags-for/ 等 24 小时健身。

Topics.txt

  • 21 世纪福克斯
  • 24 小时健身
  • 2K 游戏
  • 3M

Full Error

阅读第 1 行 24HourFitness 2https://ritetag.com/best-hashtags-for/4https://ritetag.com/best-hashtags-for/Hhttps://ritetag.com/best-hashtags-for/ohttps://ritetag.com/ best-hashtags-for/uhttps://ritetag.com/best-hashtags-for/rhttps://ritetag.com/best-hashtags-for/Fhttps://ritetag.com/best-hashtags-for/ihttps: //ritetag.com/best-hashtags-for/thttps://ritetag.com/best-hashtags-for/nhttps://ritetag.com/best-hashtags-for/ehttps://ritetag.com/best- hashtags-for/shttps://ritetag.com/best-hashtags-for/s

Traceback(最近一次调用最后一次):文件 "C:\Users\Caden\​​Desktop\Programs\LususStudios\AutoDealBot\HashtagScanner.py", 第 17 行,在 源代码 = requests.get(Link) 文件 "C:\Python34\lib\site-packages\requests-2.10.0-py3.4.egg\requests\api.py", 第 71 行,在获取 return request('get', url, params=params, **kwargs) 文件 "C:\Python34\lib\site-packages\requests-2.10.0-py3.4.egg\requests\api.py", 第 57 行,应要求提供 返回 session.request(method=method, url=url, **kwargs) 文件 "C:\Python34\lib\site-packages\requests-2.10.0-py3.4.egg\requests\sessions.py", 第 475 行,应要求提供 resp = self.send(prep, **send_kwargs) 文件 "C:\Python34\lib\site-packages\requests-2.10.0-py3.4.egg\requests\sessions.py", 第 579 行,在发送中 适配器 = self.get_adapter(url=request.url) 文件 "C:\Python34\lib\site-packages\requests-2.10.0-py3.4.egg\requests\sessions.py", 第 653 行,在 get_adapter 中 raise InvalidSchema("No connection adapters were found for '%s'" % url) requests.exceptions.InvalidSchema: 没有连接适配器 为 '2https://ritetag.com/best-hashtags-for/4https://ritetag.com/best-hashtags-for/Hhttps://ritetag.com/best-hashtags-for/ohttps://ritetag.com /best-hashtags-for/uhttps://ritetag.com/best-hashtags-for/rhttps://ritetag.com/best-hashtags-for/Fhttps://ritetag.com/best-hashtags-for/ihttps ://ritetag.com/best-hashtags-for/thttps://ritetag.com/best-hashtags-for/nhttps://ritetag.com/best-hashtags-for/ehttps://ritetag.com/best -hashtags-for/shttps://ritetag.com/best-hashtags-for/s'

【问题讨论】:

  • 使用Line = Topicfile.readlines() 一次性读取文件。去掉那条线。
  • 在底层,readlines 方法“使用”文件,因此当它返回底层文件时,位置指针位于文件末尾。然后你尝试在 for 循环中再读一些文件,但由于它已经在最后,它什么也不做。只使用这两种方法中的一种。

标签: python python-3.x for-loop readlines


【解决方案1】:

首先,python 约定将所有变量名小写。

其次,当您首先读取所有行时,您正在耗尽文件指针,然后继续循环文件。

尝试简单地打开文件,然后循环遍历它

linenumber = 0
with open("Topics.txt") as topicfile:
    for line in topicfile:
        # do work 
        linenumber += 1

然后,回溯中的问题,如果你仔细观察,你正在构建这个非常长的 url 字符串,这绝对不是 url,所以 requests 会抛出错误

InvalidSchema: No connection adapters were found for '2https://ritetag.com/best-hashtags-for/4https://ritetag.com/...

您可以调试看到Url.join(Newtopic)Newtopic 列表的每个字符之间“交错”Url 字符串,这就是str.join 的作用

【讨论】:

    【解决方案2】:

    我认为有两个问题:

    1. 您似乎在迭代 Topicfile 而不是 Topicfile.readLines()
    2. Url.join(Newtopic) 没有返回您认为的内容。 .join 接受一个列表(在这种情况下,字符串是一个字符列表)并将在每个字符之间插入Url

    以下是解决这些问题的代码:

    import requests
    
    Url = "https://ritetag.com/best-hashtags-for/"
    Topicfilename = "topics.txt"
    Topicfile = open(Topicfilename, 'r')
    Lines = Topicfile.readlines()
    Linenumber = 0
    for Line in Lines:
        Linenumber += 1
        print("Reading line", Linenumber)
    
        Topic = Line
        Newtopic = Topic.strip("\n").replace(' ', '').replace(',', '')
        print(Newtopic)
        Link = '{}{}'.format(Url, Newtopic)
        print(Link)
        Sourcecode = requests.get(Link)
    

    顺便说一句,我还建议使用小写的变量名,因为驼峰式大小写通常保留给 Python 中的类名 :)

    【讨论】:

    • 好的,谢谢!我什至没有考虑过影响for循环的两个变量的相邻性。我已将代码修改为Link = Url + Newtopic
    猜你喜欢
    • 2018-08-04
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多