1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
import http.client
import socket
from bs4 import BeautifulSoup
import sys
def getPage(page):
"""
Function to open a link, take the url of a website, and return the contain of
the website
"""
try:
# always set a timeout when you connect to an external server
url = "www.bing.com"
#connection
connection = http.client.HTTPSConnection(url, timeout=10)
#send a request
connection.request("GET", page)
#get the response on the request
response = connection.getresponse()
#reead and return the response
return response.read()
except socket.timeout:
# in a real world scenario you would probably do stuff if the
# socket goes into timeout
pass
def nbrPage ():
"""
Function to browse all resultat pagesand get their url in a list
"""
result = getPage(page)
soup = BeautifulSoup(result,features="html.parser")
liste=[]
for nbr in range(1,21,10):
liste.append("/search?q={}&first={}".format(url,nbr))
dif = 21 - nbr
if dif <= 10 :
liste.append("/search?q={}&first={}".format(url, nbr + dif))
print(liste)
return liste
def openAllResultPages():
#condition to excute an operation 7 times in same time
with PoolExecutor(max_workers=7) as executor:
#open 7 url in the same time,
for _ in executor.map(getPage, urls):
#initialise soul by getting the contain of the result
soup = BeautifulSoup(_,features="html.parser")
print(soup.findAll("cite"))
url = input("make a research : ")
page = "/search?q={}&first=11".format(url)
urls = nbrPage()
output = openAllResultPages() |
Partager