【问题标题】:Scraping all links using Python BeautifulSoup/lxml使用 Python BeautifulSoup/lxml 抓取所有链接
【发布时间】:2014-06-01 07:42:13
【问题描述】:

http://www.snapdeal.com/

我试图从这个网站上抓取所有链接,但当我这样做时,我得到了一个意想不到的结果。我发现这是因为 javascript 而发生的。

在“查看所有类别”选项卡下,您将找到所有主要产品类别。如果您将鼠标悬停在任何类别上,它将展开类别。我想要每个主要类别的链接。

url = 'http://www.snapdeal.com/'
data = urllib2.urlopen(url)
page = BeautifulSoup(data)
#print data
for link in page.findAll('a'):
       l = link.get('href')
       print l

但是,这给了我一个与我预期不同的结果(我关闭了 javascript 并查看了页面源,输出来自这个源)

我只想找到每个主要类别的所有子链接。任何建议将不胜感激。

【问题讨论】:

    标签: python python-2.7 web-scraping beautifulsoup lxml


    【解决方案1】:

    发生这种情况只是因为您让 BeautifulSoup 选择了自己最好的解析器,而您可能没有安装 lxml。

    最好的选择是使用html.parser来解析url。

    from bs4 import BeautifulSoup
    import urllib2
    url = 'http://www.snapdeal.com/'
    data = urllib2.urlopen(url).read()
    
    page = BeautifulSoup(data,'html.parser')
    
    for link in page.findAll('a'):
           l = link.get('href')
           print l  
    

    这对我有用。确保安装依赖项。

    【讨论】:

      【解决方案2】:

      Categories Menu 是您要查找的网址。许多网站使用XHR(XMLHTTPRequest) 动态生成内容。 为了检查网站的组件,请熟悉 Firefox 中的 Firebug 插件或 Chrome 中的开发者工具(内置插件)。您可以在上述插件中的网络选项卡下查看网站中使用的 XHR。

      【讨论】:

        【解决方案3】:

        我认为你应该尝试另一个库,例如selenium ,它为你提供了一个网络驱动程序,这是这个库的优势,对我来说,我无法使用 bs4 处理 javascripts。

        【讨论】:

        【解决方案4】:

        使用网络抓取工具,例如 scrapymechanize 在mechanize中,要获取snapdeal首页的所有链接,

        br=Browser()
        br.open("http://www.snapdeal.com")
        for link in browser.links():
            print link.name
            print link.url
        

        【讨论】:

          【解决方案5】:

          我一直在寻找一种方法来从仅在实际浏览器中呈现但希望使用无头浏览器运行结果的网页中抓取链接。

          我能够使用 phantomJS、selenium 和 beautiful soup 实现这一目标

          #!/usr/bin/python
          
          import bs4
          import requests
          from selenium import webdriver
          
          driver = webdriver.PhantomJS('phantomjs')
          url = 'http://www.snapdeal.com/'
          browser = driver.get(url)
          content = driver.page_source
          soup = bs4.BeautifulSoup(content)
          links = [a.attrs.get('href') for a in soup.find_all('a')]
          for paths in links:
              print paths
          driver.close()
          

          【讨论】:

            【解决方案6】:

            以下示例适用于HTTPHTTPS。我写这个答案是为了说明如何在Python 2Python 3 中使用它。

            Python 2

            这是受到this answer的启发。

            from bs4 import BeautifulSoup
            import urllib2
            url = 'https://stackoverflow.com'
            data = urllib2.urlopen(url).read()
            
            page = BeautifulSoup(data,'html.parser')
            
            for link in page.findAll('a'):
                   l = link.get('href')
                   print l 
            

            Python 3

            from bs4 import BeautifulSoup
            from urllib.request import urlopen
            import ssl
            
            # to open up HTTPS URLs
            gcontext = ssl.SSLContext()
            
            # You can give any URL here. I have given the Stack Overflow homepage
            url = 'https://stackoverflow.com'
            data = urlopen(url, context=gcontext).read()
            
            page = BeautifulSoup(data, 'html.parser')
            
            for link in page.findAll('a'):
                l = link.get('href')
                print(l)
            

            其他语言

            其他语言请见this answer

            【讨论】:

              猜你喜欢
              • 2018-07-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-08-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-12-24
              相关资源
              最近更新 更多