我在下面提到了代码;我使用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'
通过将脚本命名为lxml.py
,使其与要导入的lxml
包相同,使Python的导入机制混乱。
重命名为lxml_test_thing.py
,它将起作用。