为啥我的导入请求没有被读取? [复制]

Posted

技术标签:

【中文标题】为啥我的导入请求没有被读取? [复制]【英文标题】:Why does is my import requests not being read? [duplicate]为什么我的导入请求没有被读取? [复制] 【发布时间】:2020-10-30 19:13:13 【问题描述】:

我只是按照另一个教程做的,想自己做一个,但是请求的导入无法读取或被视为'ModuleNotFoundError:没有名为'requests'的模块。

凡是 * 是私人信息。

import requests
from bs4 import BeautifulSoup
import smtplib

URL = 'https://www.amazon.com/Samsung-Unlocked-Fingerprint-Recognition-Long-Lasting/dp/B082XXR64G/ref=redir_mobile_desktop?ie=UTF8&aaxitk=08Egp5KhvC3f6cEe802xMg&hsa_cr_id=5004960420401&ref_=sbx_be_s_sparkle_mcd_asin_0&th=1'

headers = "User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/83.0.4103.116 Safari/537.36' 

def check_price():

    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html.parser')

    title = soup.find(id="productTitle").get_text()
    price = soup.find(id="priceblock_ourprice").get_text()
    converted_price = float(price[0:5])

    if(converted_price < 750 ):
        send_mail()

    print(converted_price)
    print(title.strip())

    if(converted_price < 750):
        send_mail()



def send_mail():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.elo()
    server.starttls()
    server.elo()

    server.login('******' , '*******')

    subject = 'Price fell down!'
    body = 'Check the link: https://www.amazon.com/Samsung-Unlocked-Fingerprint-Recognition-Long-Lasting/dp/B082XXR64G/ref=redir_mobile_desktop?ie=UTF8&aaxitk=08Egp5KhvC3f6cEe802xMg&hsa_cr_id=5004960420401&ref_=sbx_be_s_sparkle_mcd_asin_0&th=1'

    msg = f"Subject: subject\n\nbody"

    server.sendmail(
    '*****',
    '*******',
    msg
    )
    print('The Email has been sent!')

    server.quit()

check_price()

【问题讨论】:

你安装了“requests”模块吗? 【参考方案1】:

我猜你还没有下载这个模块。 如果您使用的是 pycharm Preferences > Projects > Python Interpreter > 找到 Requests 模块并添加它。

或者你可以直接从终端安装模块

pip install requests

(但如果您使用不同的 ide,您需要查看可以添加模块的位置) 这样你就下载了模块来调用你编写的脚本。

我认为应该可以。

【讨论】:

以上是关于为啥我的导入请求没有被读取? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 Tomcat 部署的 web.xml 没有被主机服务器读取?

为啥我的 for 循环只读取我创建的数组的最后一个元素? [复制]

为啥html页面不读取php页面

在 python 中,为啥从数组读取比从列表读取慢?

为啥颤振无法导入我的模型?

django导出excel文件的内容为啥是空的