簡體   English   中英

AttributeError: __enter__: requests.get Python

[英]AttributeError: __enter__: requests.get Python

我正在嘗試解析這個網站: https : //decrypt.co/14493/the-12-dapps-of-christmas並獲得一個標題。 但是,我在with requests.get(url) as urlobj:上收到AttributeError: __enter__錯誤。

    def get_title(self, url):
        # gets the title of the article from the web
        print("get_title url: " + url)  //get_title url: https://decrypt.co/14493/the-12-dapps-of-christmas
        with requests.get(url) as urlobj:  //error here
            soup = bs(urlobj.content, "html.parser")
            title = soup.find("h1", class_="Headings__H1-sc-1ibq1fi-0 PostHeader__Heading-sc-1cg3u6k-4 fXgBJm").text
            title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').decode("utf-8")
            return title

我的要求.txt:

requests==2.22.0
beautifulsoup4==4.8.1
Flask==1.1.1
flask_wtf==0.14.2
praw==6.4.0
WTForms==2.2.1
py -3.6 -m pip list

給出了這個結果:

在此處輸入圖片說明

我從 PyCharm 的 Python3.6 解釋器或使用py -3.6 TelegramBot.py命令運行程序。

編輯

我更新請求,現在得到

    title = soup.find("h1", class_="Headings__H1-sc-1ibq1fi-0 PostHeader__Heading-sc-1cg3u6k-4 fXgBJm").text
AttributeError: 'NoneType' object has no attribute 'text'

出錯

title = soup.find("h1", class_="Headings__H1-sc-1ibq1fi-0 PostHeader__Heading-sc-1cg3u6k-4 fXgBJm").text

這是否意味着它找不到具有指定類的對象?

import requests
from bs4 import BeautifulSoup

r = requests.get("https://decrypt.co/14493/the-12-dapps-of-christmas")
soup = BeautifulSoup(r.text, 'html.parser')

for item in soup.findAll("h2", {'class': 'sc-kpOJdX bgcxqD'}):
    print(item.text)

輸出:

1. MyCrypto Heroes
2. Arcane Maps
3. ForkDelta
4. Synthetix
5. Photo Vault
6. NewDex
7. Compound
8. Dice
9. RocketGame
10. Maker
11. Crypto Legends
12. Graphite Docs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM