当我们在 Python 中使用请求时,一些 html 代码消失了

Some html code dissapears when we use request in Python

我正在从网站上抓取一些数据。我需要从产品列表中恢复一些 link。首先,我用检查元素确定了 link 之一:

然后我用request将该页面的所有源代码保存在一个文本文件中:

source_code = requests.get(link)
plain_text= source_code.txt

然后我用我的文本编辑器搜索 link 但没有找到它。我正在使用 BeautifulSoup4,但我已经尝试了几种 different 方法来抓取页面以获取产品列表,但所有方法都给出相同的结果。

我怀疑产品列表是在有人进入页面时由一些代码(可能是Java)生成的,但我不确定。我已经花了几个小时来尝试完成这项工作,因此我们将不胜感激任何提示。

Python逗我不停。我找到了一个使用 PhantomJS 的 Python 库。它允许我们在 python 程序中 运行 Java 脚本代码。经过大量工作,我将回答我自己的问题:

from ghost import Ghost
import re

def filterProductLinks(links):  #filter the useless links using regex
   pLinks= list()
   for l in links:
      if re.match(".*productDetails.*",str(l)):
         pLinks.append(l)
   return pLinks #List of item url(40 max)

def getProductLinks(url):   #get the links generated by Java code
   ghost = Ghost(wait_timeout=100)
   ghost.open(url)
   links = ghost.evaluate("""
                    var links = document.querySelectorAll("a");
                    var listRet = [];
                    for (var i=0; i<links.length; i++){
                        listRet.push(links[i].href);
                    }
                    listRet;
                """)
   pLinks= filterProductLinks(links[0])
   return pLinks

#Test
pLinks= getProductLinks('http://www.lider.cl/walmart/catalog/category.jsp?id=CF_Nivel3_000042&pId=CF_Nivel1_000003&navAction=jump&navCount=0#categoryCategory=CF_Nivel3_000042&pageSizeCategory=20&currentPageCategory=1&currentGroupCategory=1&orderByCategory=lowestPrice&lowerLimitCategory=0&upperLimitCategory=0&&504')
for l in pLinks:
   print l
print len(pLinks)

Java 代码不是我的。我从 Ghost.py 文档页面获取它:Ghost.py Documentation