【发布时间】: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