Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用请求时,一些html代码会消失_Python_Html_Python 2.7_Web Crawler - Fatal编程技术网

在Python中使用请求时,一些html代码会消失

在Python中使用请求时,一些html代码会消失,python,html,python-2.7,web-crawler,Python,Html,Python 2.7,Web Crawler,我正在从一个网站上抓取一些数据。我需要从产品列表中恢复一些链接。首先,我确定了与inspect元素的一个链接: 然后我使用request将该页面的所有源代码保存在一个文本文件中: source_code = requests.get(link) plain_text= source_code.txt 然后我用我的文本编辑器搜索链接,但它没有找到它。我正在使用BeautifulSoup4,但我已经尝试了几种方法来抓取页面以获得产品列表,但都给出了相同的结果 我怀疑当有人进入页面时,产品列表是由

我正在从一个网站上抓取一些数据。我需要从产品列表中恢复一些链接。首先,我确定了与inspect元素的一个链接:

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

source_code = requests.get(link)
plain_text= source_code.txt
然后我用我的文本编辑器搜索链接,但它没有找到它。我正在使用BeautifulSoup4,但我已经尝试了几种方法来抓取页面以获得产品列表,但都给出了相同的结果


我怀疑当有人进入页面时,产品列表是由一些代码(可能是Java)生成的,但我不确定。我已经花了好几个小时试着做这件事,所以任何暗示都会得到赞赏

巨蟒从不停下来逗我开心。我发现了一个使用PhantomJS的Python库。它允许我们在python程序中运行JavaScript代码。在做了大量工作之后,我将回答我自己的问题:

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)
从重影导入重影
进口稀土
def filterProductLinks(links):#使用正则表达式过滤无用的链接
pLinks=list()
对于l in链接:
如果重新匹配(“productDetails.*”,str(l)):
pLinks.append(l)
返回链接#项目url列表(最多40个)
def getProductLinks(url):#获取Java代码生成的链接
重影=重影(等待超时=100)
ghost.open(url)
links=ghost.evaluate(“”)
var links=document.querySelectorAll(“a”);
var listRet=[];

对于(var i=0;iPython从不停下来逗我开心。我发现了一个使用PhantomJS的Python库。它允许我们在Python程序中运行JavaScript代码。在做了大量工作后,我将回答我自己的问题:

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)
从重影导入重影
进口稀土
def filterProductLinks(links):#使用正则表达式过滤无用的链接
pLinks=list()
对于l in链接:
如果重新匹配(“productDetails.*”,str(l)):
pLinks.append(l)
返回链接#项目url列表(最多40个)
def getProductLinks(url):#获取Java代码生成的链接
重影=重影(等待超时=100)
ghost.open(url)
links=ghost.evaluate(“”)
var links=document.querySelectorAll(“a”);
var listRet=[];

对于(var i=0;iit几乎肯定是由javascript创建的…尝试使用phantomjs或seleniumCan之类的工具我可以仅使用python解决此问题吗?没有python无法为您运行javascript代码…是的,它可以。感谢phantomjs参考。它几乎肯定是由javascript创建的…尝试使用phantomjs或seleniumCan之类的工具我可以解决此问题仅使用python?没有python不能为您运行javascript代码…是的,它可以。感谢PhantomJS参考。