AttributeError:__ enter __:requests.get Python

时间:2019-12-26 16:16:31

标签: python python-3.x beautifulsoup python-requests

我正在尝试解析以下站点:https://decrypt.co/14493/the-12-dapps-of-christmas并获得标题。但是在行AttributeError: __enter__上出现with requests.get(url) as urlobj:错误。

    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

给出以下结果:

enter image description here

然后我从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

是否表示找不到指定类的对象?

1 个答案:

答案 0 :(得分:0)

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