【问题标题】:This code only runs on python2.7?这段代码只在python2.7上运行?
【发布时间】:2017-07-10 11:19:32
【问题描述】:

编辑我刚刚弄清楚 map() 函数是什么导致它无法正常运行我不知道为什么,但至少它现在可以工作了,感谢所有帮助:)

我刚刚通过 pycharm 将我的代码更新为 pep8 代码样式指南,但现在当我运行它时它只在 python2 上运行,而当 python3 出现此错误时。

Traceback (most recent call last):
  File "main.py", line 28, in <module>
    soup.find_all("td", {"class": "location"})[1:],soup.find_all("td", {"class": "date-time"})[1:]):
TypeError: 'NoneType' object is not callable

跟踪号也不是我的,我只是在互联网上找到的。 导入系统 导入请求 从 bs4 导入 BeautifulSoup

s = requests.Session()
s.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36 '
#if len(sys.argv) == 2:
#    trackingNumber = sys.argv[1]
#else:
#    print("Enter a tracking number to track a number.")
#    sys.exit()
r = s.get("https://tools.usps.com/go/TrackConfirmAction.action?tRef=fullpage&tLc=1&text28777=&tLabels=CX263292019US")
soup = BeautifulSoup(r.text, "lxml")



# Variable declaration.
current_status = soup.find(class_="detail-summary", )
some = soup.find_all("div", {"id": "tracking-results"})

for tag in some:
    divTags = tag.find_all("li")
    for tag in divTags:
        testrip = tag.encode("utf-8").strip()  # Remove all the white space off the text from bs4.
        if testrip == "The Postal Service could not locate the tracking information for your request." \
                      " Please verify your tracking number and try again later.":
            break

print(
    "----------------------------------------------------------------------------------------------------------------")

print(current_status.get_text().strip())
for Status, Location, Time in map(None, soup.find_all("span", {"class": "info-text"}),
                                  soup.find_all("td", {"class": "location"})[1:],
                                  soup.find_all("td", {"class": "date-time"})[1:]):

    print(
        "----------------------------------------------------------------------------------------------------------------")
    try:
        print(Status.get_text().strip())
        print(Location.get_text().strip())
        print(" ".join(Time.get_text().split()))
    except Exception:
        pass
print(
    "----------------------------------------------------------------------------------------------------------------")

【问题讨论】:

  • 它在 Python3 上究竟是如何失败的?
  • 你想用map(None, ...)做什么?
  • 我正在迭代 bs4 给我的信息,没有保留它会在一个列表用完时停止执行,但是在列表中添加了一个额外的项目,所以我用切片进行了修复。

标签: python python-3.x web-scraping beautifulsoup code-formatting


【解决方案1】:

快速但短期的可持续解决方案:

  • 添加from __future__ import print_function(现在使用Python3语法)
  • 检查导入是否在两个 Python 下都有效? (在大多数情况下,它们默认使用 Python,但谁知道你的发行版是什么)
  • 如果上述方法均无效,请发布堆栈跟踪

更长期的解决方案:Pycharm 中的turn on Python 3 syntax checks

设置 -> 编辑器 -> 检查 -> Python -> 代码兼容性检查

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2020-05-09
    • 1970-01-01
    • 2019-03-05
    相关资源
    最近更新 更多