当我运行此代码时,它返回“ []”。我怎样才能解决这个问题?

时间:2019-01-08 20:48:23

标签: python web-scraping beautifulsoup python-requests

我在stackoverflow中的第一个问题。我是python的初学者,我想请求喜欢任何instagram照片,但我的代码返回空

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

我想看到喜欢这张照片但不喜欢的人的名字。

1 个答案:

答案 0 :(得分:3)

首先,要进行抓图,应使用像Anaconda这样的预编译库。在这里下载它:https://www.anaconda.com/download/,并记住python可执行文件的路径在哪里。

因为instagram使用javascript,所以您返回的列表为空。请求无法为您将javascript呈现为html,因此您需要使用更强大的方法,例如硒。

尝试这样的事情:

安装硒

在您的终端中

conda install selenium

下载Chromedriver

http://chromedriver.chromium.org/downloads

将硒导入您的代码

import os  
from selenium import webdriver  
from selenium.webdriver.common.keys import Keys  
from selenium.webdriver.chrome.options import Options  
from bs4 import BeautifulSoup

chrome_options = Options()  
chrome_options.add_argument("--headless")  

driver = webdriver.Chrome(executable_path="path-to-chromedriver",chrome_options=chrome_options)  
driver.get("https://www.instagram.com/p/BsYt_megGfN/")

html_source = driver.page_source  
driver.quit()

soup = BeautifulSoup(html_source,"html.parser")
data = soup.findAll("div",{"class","Nm9Fw"})
print(comments) # syntax for printing changes here for Python3

使用您的Anaconda版本的python运行此程序。

相关问题