python-3.x - Python错误,我做得完全正确,但出现错误

标签 python-3.x web-crawler syntax-error

我正在用python构建网络爬虫。它正在爬新闻站点。但是当我收到此错误时:TabError:缩进中的制表符和空格使用不一致。它在我的脚本的第28行中。我不知道如何解决。

    import requests
    from lxml import html
    import time
    from colorama import *

    def crawl():
        URL = "http://www.svt.se/nyheter/"
        host = "http://www.svt.se"
        blankHTML = requests.get(URL)
        tree = html.fromstring(blankHTML.text)
        Nyheter = tree.xpath('//span[@class="nyh_teaser__heading-title"]/text()')
        beskrivning = tree.xpath('//span[@class="nyh_teaser__text"]/text()')
        link = tree.xpath('//a[@class="nyh_teaser__link"]/@href')
        link_list = []
        newsnumber = 0
        numbersOfNews = 0
        for numb in range(1,10):
            print(Fore.GREEN + "Titel: " + Nyheter[newsnumber])
            print(Fore.YELLOW + "Beskrivning: " + beskrivning[newsnumber])
            print(link[newsnumber])
            link_list.append(link)
            newsnumber += 1
        choice1 = input("<News> ").lower()
        while True:
            if choicel == 1:
                URL = host + link_list[0]
                blankHTMLS = requests.get(URL)
                treek = html.fromstring(blankHTMLS.text)
                wholeNew = treek.xpath('//div[@class="nyh_article__body]/p/text()')
                print(wholeNew)






    crawl()

最佳答案

您的错误将由缩进的制表符和空格引起。当您在此处粘贴代码示例时,它们丢失了,所以我无法重现该问题。您可以使用you can't mix them in the same code block之一。您可以尝试使用Notepad++之类的文件打开python文件并启用View > Show Symbol > Show White Spaces and TAB

我还注意到其他一些错误可能会使您绊倒。您有一个无限循环,因此您的程序将永远无法完成。另外,您还创建了一个名为choice1的变量,但稍后将其称为choicel会导致错误。

关于python-3.x - Python错误,我做得完全正确,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36869653/

相关文章:

python - 网页中的名称不会被抓取

Facebook 爬虫机器人崩溃网站

javascript - Node JS 不显示缓冲区中的 UTF-8 字符

Vb.net:调用与原型(prototype)不匹配的函数通过编译器检查

Perl 使用 qx 中的 find

Python 串行 (pySerial) 使用 EOL\r 而不是\n 读取行

python - 使用 TypeVar 作为 Callable 的参数时出现问题

python - Pygame - 在给定区域随机生成对象

python 3.6.3 未声明长度列表的索引错误

Python 3.2 回文