我有一个Python库存更新脚本,该脚本每晚运行一次,从网站上提取库存。我最近开始遇到问题,经过进一步调查发现,当我通过Web浏览器(查看源代码)查看源内容时,它看起来很正常。但是,当我使用python将其打印到控制台时,它看起来很奇怪(并且正在破坏脚本)。想知道是否有人看到过这样的东西或知道是什么原因造成的?
网络浏览器显示以下内容(已编辑的网址):
<ul class='vnav vnav__subnav vnav--level2'>
<li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Folding Tables</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Bookcases</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Printer Stands</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Computer Desks</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Office Chairs</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Filing Cabinets</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Letter Holders</a>
</li></ul>
</li>
<li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Bathroom</a>
<ul class='vnav vnav__subnav vnav--level2'>
<li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Bathroom Mirrors</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Bathroom Sinks</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Bathroom Cabinets</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Bathroom Vanities</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Laundry Hampers</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Bath Towel Sets</a>
</li><li class='vnav__item'><a href='https://xx.htm' class='vnav__link'>Shower Curtains</a>
</li></ul>
但是控制台中的Python print()显示以下内容(已编辑的URL):
<ul class="vnav vnav__subnav vnav--level2">
<li class="vnav__item"><a class="vnav__link" href="https://xx.htm">Folding Tables</a>
</li><li class="vnav__item"><a class="vnav__link" href="https://xx.htm">Bookcases</a>
</li><li class="vnav__item"><a class="vnav__link" href="https://xx.htm">Printer Stands</a>
</li><li class="vnav__item"><a class="vnav__link" href="https://xx.htm">Computer Desks</a>
</li><li class="vnav__item"><a class="vnav__link" href="https://xx.htm">Office Chairs</a>
</li><li class="vnav__item"><a class="vnav__link" href="https://xx.htm">Filing Cabinets</a>
</li><li class="vnav__item"><a class="vnav__link" href="https://xx.htm">Letter Holders</a>
</li></ul>
</li>
<li class="vnav__item"><a href="https:">/ / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > B a t h r o o m / a >
u l c l a s s = ' v n a v v n a v _ _ s u b n a v v n a v - - l e v e l 2 ' >
l i c l a s s = ' v n a v _ _ i t e m ' > a h r e f = ' h t t p s : / / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > B a t h r o o m M i r r o r s / a >
/ l i > l i c l a s s = ' v n a v _ _ i t e m ' > a h r e f = ' h t t p s : / / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > B a t h r o m S i n k s / a >
/ l i > l i c l a s s = ' v n a v _ _ i t e m ' > a h r e f = ' h t t p s : / / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > B a h r o o m C a b i n e t s / a >
/ l i > l i c l a s s = ' v n a v _ _ i t e m ' > a h r e f = ' h t t p s : / / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > B a h r o o m V a n i t i e s / a >
/ l i > l i c l a s s = ' v n a v _ _ i t e m ' > a h r e f = ' h t t p s : / / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > L a u n r y H a m p e r s / a >
/ l i > l i c l a s s = ' v n a v _ _ i t e m ' > a h r e f = ' h t t p s : / / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > B a t h T o w e l S e t s / a >
/ l i > l i c l a s s = ' v n a v _ _ i t e m ' > a h r e f = ' h t t p s : / / x x . h t m ' c l a s s = ' v n a v _ _ l i n k ' > S h o w e C u r t a i n s / a >
/ l i > / u l >
在网络浏览器中,内容类型为“ text / html”,编码为“ ISO-8859-1”,但通过Python打印时显示为“ UTF-8”。另外,在Python控制台print()上,html的其余全部与所有空格和字符一起出现,除了结尾处的右边,它恢复了正常(除了看起来有2个标记,这是一个不同的问题):
/ b o d y >
/ h t m l >
</a></li></ul></div></nav></body></html>
最后,如果我尝试使用UTF-8而不是ISO-8859-1进行解码,则会出现以下错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 74396: invalid continuation byte
答案 0 :(得分:0)
没关系,想通了。
最重要的提示:在不同的VirtualEnvs中工作时,请始终确保您的python版本是相同的。本来我没有检查,但是当我来回跳来跳去时,才决定进行验证。我假设正在使用的python版本没有使用。一旦我进行了切换...是的!更好。