简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'getText'

I took a udemy course where instructor teach about how to get user tweet.

when i run code it gives me

AttributeError: 'NoneType' object has no attribute 'getText'

not getting good support on udemy so how to make this code work.

from bs4 import BeautifulSoup as soupy

import urllib.request
import re

html = urllib.request.urlopen('https://twitter.com/samad_bloch0x1').read()

soup = soupy(html, features="html.parser",)

x = soup.find('p', {'class':'js-tweet-text'}).getText()

filter = re.findall(r'"(.*)"', x)ut
tweet = filter[0]  

print(tweet)

This is my whole code

What happens?

First at all, take a closer look into your soup - There is the truth.

None is returned, cause beautifulsoup could not find the element you try to select - So are you sure there is an element with such this class js-tweet-text in the source and the soup?

Follwowing could also be a reason:

JavaScript is not available.

We've detected that JavaScript is disabled in this browser. Please enable JavaScript or switch to a supported browser to continue using twitter.com. You can see a list of supported browsers in our Help Center.

What can you do?

  • use offical api

  • use proxies

  • try to use selenium

here is the original source code from udemy instructor:

from bs4 import BeautifulSoup as soupy
import urllib.request
import re

html =urllib.request.urlopen('https://twitter.com/HussamKhrais').read()

soup = soupy(html, features="html.parser")


x = soup.find("meta", {"name":"description"})['content']


filter = re.findall(r'"(.*)"', x)

tweet = filter[0]  


print(tweet)

this is also doesnt working it throw this error:

x = soup.find("meta", {"name":"description"})['content']

TypeError: 'NoneType' object is not subscriptable

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