-3

编辑我只是想出了它是什么地图()函数是什么导致它无法正常运行我不知道为什么,但至少它的工作现在感谢所有的帮助:)此代码只能在python2.7上运行?

我刚刚更新我的代码通过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 进口SYS 导入请求

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(
    "----------------------------------------------------------------------------------------------------------------") 
+1

它究竟是如何失败的Python3? – Marat

+0

你想用'map(None,...)'来做什么? –

+0

我正在迭代bs4给我的信息,没有保留它会在一个列表用完时停止执行,所以我在列表中添加了一个额外的项目,所以我使用这个切片进行了修复。 –

回答

0

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

  • 添加from __future__ import print_function(它现在使用Python3语法)
  • 检查,如果进口在两个工作蟒蛇? (在大多数情况下,他们与Python来默认,但谁知道什么是你的发行版)
  • 后的堆栈跟踪,如果没有上述工作

更长期的解决办法:在turn on Python 3 syntax checks Pycharm:

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

相关问题