links aus HTML-quelltext extrahieren



  • Moin moin,

    ich versuche alle links aus einem html-quelltext zu extrahieren. Meine funktion sieht so aus:

    vector <string> found;
    
    void find(string quelltext)
    {
        string link, src; 
        int pos, pos2;
    
       while(quelltext.find("href=\"") != -1)
        {
        pos = quelltext.find("href=\"");
        quelltext = quelltext.substr(pos+6, quelltext.length());
    
        pos2 = quelltext.find("\"");
        link = quelltext.substr(0,pos2);
    
        if(std::find(found.begin(), found.end(),link) == found.end() )
        {
           cout<<link<<endl<<endl;
           found.push_back(link);
    
          src = GetSource(link);
          if(src != "")
    
        }  
    
       quelltext = quelltext.substr(pos, quelltext.length()); 
      }
    }
    

    Leider werden nicht alle links angezeigt. Kann mir jemand sagen, wo der fehler liegt oder mir einen andere funktion geben ?

    Danke, 👍
    Wiesel



  • vector <string> found;
    
    void find(string quelltext)
    {
        string link, src; 
        int pos, pos2;
    
       while( (pos = quelltext.find("href=\"")) != -1) 
        {
        quelltext = quelltext.substr(pos+6, quelltext.length());
    
        pos2 = quelltext.find("\"");
        link = quelltext.substr(0,pos2);
    
        if(std::find(found.begin(), found.end(),link) == found.end() )
        {
           cout<<link<<endl<<endl;
           found.push_back(link);
    
          src = GetSource(link);
          if(src != "")
    
        }  
    
       quelltext = quelltext.substr(pos, quelltext.length()); 
      }
    }
    

    Versuchs jetzt mal, mehr kann ich auch nicht sagen. Ich weiß nicht wie deine Klasse definiert ist...



  • Machs doch mit regulären Ausdrücken (Boost::Regex)

    Vereinfach die Abdeckung von spaces, weil dein code würde

    <a href = "http://web.de/">link</a>
    

    nicht erkennen - die spaces um das "=" verhindern das ...

    mfg Branleb



  • #include <iostream>
    #include <string>
    #include <vector>
    
    typedef std::vector<std::string> StrVector;
    
    StrVector find(const std::string& source);
    
    int main()
    {
    	const std::string source = "<a href\n=\n\"www.test.de\">Bla</a><a href\n=\n\"www.test2.de\">Bla</a><a href\n=\n\"www.12312.de\"  >Bla</a>";
    
    	StrVector links = find(source);
    
    	std::cout << "Found " << links.size() << " link(s)\n\n";
    
    	for (StrVector::iterator it = links.begin(); it != links.end(); ++it)
    	{
    		std::cout << *it << std::endl;
    	}
    }
    
    StrVector find(const std::string& source)
    {
    	StrVector vec;
    
    	size_t pos = 0;
    
    	while ((pos = source.find("href", pos + 1)) != std::string::npos)
    	{
    		for (unsigned int i = pos; i < source.length(); ++i)
    		{
    			if (source[i] == '"')
    			{
    				std::string buf;
    
    				++i;
    
    				while (source[i] != '"' && i < source.length())
    				{
    					buf += source[i++];
    				}
    
    				vec.push_back(buf);
    
    				break;
    			}
    		}
    	}
    
    	return vec;
    }
    


  • Regular Expressions helfen bei solchen Sachen auch immer recht gut, wenn man etwas komplexere Suchpatterns hat.


Anmelden zum Antworten