Discussione Web-scraper non funzionante

  • Autore discussione Utente cancellato 275459
  • Data d'inizio
U

Utente cancellato 275459

Salve gente sto creando un web scraper con bs4 solo che non funziona e non so qual'è l'errore.
Codice:
Python:
from bs4 import BeautifulSoup
import bs4
import requests, lxml
import os
import selenium
from selenium import webdriver
from selenium.webdriver import firefox

global link
link="https://www.shodan.io/search?query=ftp"

def ftp():
    os.system("wget "+link)
    os.system("mv search?query=ftp .index.html")
    files=open(".index.html","r")
    html=files.read()
    soup=BeautifulSoup(html, 'html.parser')
    result=soup.find_all("div", class_="ip")
    for a in result:
        print(a.get_attribute("href"))
    files=close()
    os.system("rm .index.html")

if __name__ == "__main__":
    ftp()
 
Salve gente sto creando un web scraper con bs4 solo che non funziona e non so qual'è l'errore.
Codice:
Python:
from bs4 import BeautifulSoup
import bs4
import requests, lxml
import os
import selenium
from selenium import webdriver
from selenium.webdriver import firefox

global link
link="https://www.shodan.io/search?query=ftp"

def ftp():
    os.system("wget "+link)
    os.system("mv search?query=ftp .index.html")
    files=open(".index.html","r")
    html=files.read()
    soup=BeautifulSoup(html, 'html.parser')
    result=soup.find_all("div", class_="ip")
    for a in result:
        print(a.get_attribute("href"))
    files=close()
    os.system("rm .index.html")

if __name__ == "__main__":
    ftp()
Ciao!
Ti consiglio di non scaricare il codice sorgente della pagina, ma di usare requests per connetterti (camuffando il campo "User-Agent" dell'heder). Successivamente itera con il ciclo for sul risultato del metodo select(cssSelector) di BeautifulSoup e get("href").
 
Salve gente sto creando un web scraper con bs4 solo che non funziona e non so qual'è l'errore.
Codice:
Python:
from bs4 import BeautifulSoup
import bs4
import requests, lxml
import os
import selenium
from selenium import webdriver
from selenium.webdriver import firefox

global link
link="https://www.shodan.io/search?query=ftp"

def ftp():
    os.system("wget "+link)
    os.system("mv search?query=ftp .index.html")
    files=open(".index.html","r")
    html=files.read()
    soup=BeautifulSoup(html, 'html.parser')
    result=soup.find_all("div", class_="ip")
    for a in result:
        print(a.get_attribute("href"))
    files=close()
    os.system("rm .index.html")

if __name__ == "__main__":
    ftp()
Prova a fare così:
Python:
def ftp():
    headers={"User-Agent":"Fake_user_agent"};
    r=requests.get(url,headers=headers);
    soup=bs4.BeautifulSoup(r.text,"html.parser");
    result=soup.find_all("div",class_="ip");
    tag="";
    for tag_ in result.contents:
        tag+=tag_;
    soup_=bs4.BeautifulSoup(tag, "html.parser");
    result_=soup.find_all("a");
    for tags in result_:
        print(tags.contents[0]);

includere selenium e quelle altre librerie non ti serve ti basta includere bs4 e requests.