我对所有编程知识都很陌生,并且正在为我的社会工程项目学习Python。非常抱歉,如果您碰到自己的额头。
所以现在我在看一个教程,以从某个instagram页面中抓取某些信息。让我们说我想从www.instagram.com/nbamemes提取信息
我在第12行“ IndentationError:预期缩进的块”中遇到问题。所以我已经用谷歌搜索了,但是我没有得到代码。我需要在哪里放置我自己的信息的占位符在哪里。
import requests
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json
class insta_Scraper_v1:
def getinfo(self, url):
html = urllib.request.urlopen('www.instagram.com/nbamemes', context=self.ctx).read()
soup = BeautifulSoup(html, 'html.parser')
data = soup.find_all('meta', attr={'property': 'og:description'})
text = data[0]
user = '%s %s %s' % (text[-3], text[-2], text[-1])
followers = text[0]
following = text[2]
posts = text[4]
print('User:', user)
print('Followers:', followers)
print('Following:', following)
print('Posts:', posts)
print('-----------------------')
def mail(self):
self.ctx = ssl.create_default_context()
self.ctx.check_hostname = False
self.ctx.verify_mode = ssl.CERT_NONE
with open('123.txt') as f:
self.content = f.readlines()
self.content = [x.strip() for x in self.content]
for url in self.content:
self.getinfo(url)
if __name__ == '__main__'
obj = insta_Scraper_v1()
obj.mail()
我使用了一个教程对此进行编程。但是,我没有把整个事情做好。它对初学者并不完全友好,我似乎需要帮助。再次对这个超级初学者的问题感到抱歉。
问候,
lev
答案 0 :(得分:1)
将来,共享由代码产生的错误消息将很有用。它包括发生错误的行。
根据您提供的代码,我可以看到您没有缩进函数中的代码。在函数声明def
之后,您需要缩进其中的所有代码
所以来自:
def getinfo (self, url):
html = urllib.request.urlopen('www.instagram.com/nbamemes', context=self.ctx).read()
soup = BeautifulSoup(html, 'html.parser')
data = soup.find_all ('meta', attr={'property': 'og:description'})
收件人:
def getinfo (self, url):
html = urllib.request.urlopen('www.instagram.com/nbamemes', context=self.ctx).read()
soup = BeautifulSoup(html, 'html.parser')
data = soup.find_all ('meta', attr={'property': 'og:description'})
答案 1 :(得分:0)
缩进是python中的块分隔符。下面是缩进代码。每当使用条件循环,def和类时,都将创建一个块。为了定义您必须使用空格缩进代码。通常最好使用制表符空间,但即使是单个空格也可以正常工作。
import requests
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json
class insta_Scraper_v1:
def getinfo (self, url):
html = urllib.request.urlopen('www.instagram.com/nbamemes', context=self.ctx).read()
soup = BeautifulSoup(html, 'html.parser')
data = soup.find_all ('meta', attr={'property': 'og:description'})
text = data[0]
user = '%s %s %s' % (test[-3], text[-2], text[-1])
followers = text[0]
following = text[2]
posts = text[4]
print ('User:', user)
print ( 'Followers:', followers)
print ('Following:', following)
print ('Posts:', posts)
print ('-----------------------')
def mail(self:
self.ctx = ssl.create_default_context()
self.ctx.check_hostname = False
self.ctx.verify_mode = ssl.CERT_NONE
with open('123.txt') as f:
self.content = f.readlines()
self.content = [x.strip() for x in self.content]
for url in self.content:
self.getinfo(url)
if __name__ == '__main__'
obj = insta_Scraper_v1()
obj.main()
Ref:Geeks For Geeks : Indentation
谢谢