如何在窗口10中解决模块问题?

问题描述 投票:0回答:1

我在下面提到了代码;我使用python 3.7 idle运行此代码,该代码成功运行,但是当我将其另存为file.py并通过使用cmd运行它时,会弹出import module错误。

我的代码:

import requests 
from lxml import html
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'}
# url to scrape data from 
link = 'https://www.bhaskar.com/sports/'

# path to particular element 
path = '//*[@id="top-nav1"]'

response = requests.get(link,headers) 
byte_string = response.content 

# get filtered source code 
source_code = html.fromstring(byte_string) 
print(source_code)
# jump to preferred html element 
tree = source_code.xpath(path) 
print(tree.text_content())

错误:无法从'lxml'导入名称'html'

我不明白当两者都在相同的python文件上运行时,为什么会弹出此类错误!!!In this image you can see no error thrown

i saved code as lxml.py , throws error while executing

python python-3.x lxml
1个回答
0
投票

通过将脚本命名为lxml.py,使其与要导入的lxml包相同,使Python的导入机制混乱。

重命名为lxml_test_thing.py,它将起作用。

© www.soinside.com 2019 - 2024. All rights reserved.