【发布时间】:2019-10-24 19:49:14
【问题描述】:
我目前正在使用 python 从 instagram 上的用户那里收集信息,使用包含 instagram 用户链接的文本文件。虽然我可以收集关注者的数量、关注的数量和帖子的数量,但我希望能够从用户那里收集生物信息。收集生物信息将使我最终能够解析这些信息并收集电子邮件。我能做到这一点的最好和最简单的方法是什么?
我在 Python 方面没有经验,所以我从互联网上获取了一个示例代码。我试图分析代码并使用我所知道的来修改它以满足我的需要,但没有结果。
import requests
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
import json
class Insta_Info_Scraper:
def getinfo(self, url):
html = urllib.request.urlopen(url, context=self.ctx).read()
soup = BeautifulSoup(html, 'html.parser')
data = soup.find_all('meta', attrs= {'property':'og:description'})
text = data[0].get('content').split()
user = '%s %s %s' % (text[-3], text[-2], text[-1])
followers = text[0]
following = text[2]
posts = text[4]
email = ""
print ('User:', user)
print ('Followers:', followers)
print ('Following:', following)
print ('Posts:', posts)
print ('Email:', email)
print ('---------------------------')
def main(self):
self.ctx = ssl.create_default_context()
self.ctx.check_hostname = False
self.ctx.verify_mode = ssl.CERT_NONE
with open('users.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_Info_Scraper()
obj.main()
目前,我将一个空字符串作为“电子邮件”变量的值,但最终希望将其替换为可以从特定用户那里获取电子邮件的代码。
【问题讨论】:
-
你好@rivas142。很高兴您包含并格式化了代码。您还应该在此处查看有关格式化的更多信息:format your code。
-
它的工作,但不是所有的帖子都只能得到最后12个帖子,那么如何获得所有的帖子
标签: python beautifulsoup instagram screen-scraping