简体   繁体   中英

How to solve module problem in Windows 10?

I have a code as below.

I run this code using python 3.7 idle, which runs successfully. But when I save it as file.py and run it from using cmd, it pops import module error.

My code:

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())

Error: cannot import name 'html' from 'lxml'

I can not understand when both are running on the same python files why such Error pop!!! 在此图像中,您可以看到没有抛出错误

我将代码保存为 lxml.py,执行时抛出错误

You're confusing Python's import machinery by having named your script lxml.py , the same name as the lxml package you're importing.

Rename it to, say lxml_test_thing.py and it'll work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM