繁体   English   中英

python中的for循环出现readlines()错误

[英]readlines() error with for-loop in python

这个错误很难描述,因为我无法弄清楚循环如何影响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行24小时健身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-对于/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

追溯(最近一次通话):文件“ C:\\ Users \\ Caden \\ Desktop \\ Programs \\ LususStudios \\ AutoDealBot \\ HashtagScanner.py”,源代码中的第17行= request.get(Link)文件“ C:\\ Python34 \\ lib \\ site-packages \\ requests-2.10.0-py3.4.egg \\ requests \\ api.py“,第71行,在get return请求中(“ get”,url,params = params,** kwargs)文件“ C: \\ Python34 \\ lib \\ site-packages \\ requests-2.10.0-py3.4.egg \\ requests \\ api.py“,第57行,在请求返回会话中。request(方法=方法,url = url,** kwargs)在请求resp = self.send(prep,** send_kwargs)中的文件“ C:\\ Python34 \\ lib \\ site-packages \\ requests-2.10.0-py3.4.egg \\ requests \\ sessions.py”,第475行发送适配器= self.get_adapter(url = request.url)文件中的“ C:\\ Python34 \\ lib \\ site-packages \\ requests-2.10.0-py3.4.egg \\ requests \\ sessions.py”,行579 C:\\ Python34 \\ lib \\ site-packages \\ requests-2.10.0-py3.4.egg \\ requests \\ sessions.py“,第653行,位于get_adapter中,引发InvalidSchema(”未找到“%s”的连接适配器” %url)request.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-对于/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'

我认为有两个问题:

  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中为类名保留:)

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

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

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

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

然后,回溯中的问题,如果仔细观察,您正在构建这个非常长的url字符串,而且绝对不是URL,因此请求会引发错误

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

您可以调试以查看Url.join(Newtopic)Newtopic列表的每个字符之间“交织” Url字符串,这是str.join要做的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM