Best Python code snippet using SeleniumBase
webtools.py
Source:webtools.py
...5import subprocess6from datetime import datetime78class webinspector:9 def inspect_html(webcontent,domain):1011 ##domain = 'www.ahzixun.cn'121314 ##correct web file systems15 stripped = re.sub('<img src="/', '<img src="', webcontent)16 stripped = re.sub('content=', 'content="', stripped)17 stripped = re.sub('=""', '="', stripped)18 stripped = re.sub('//' + domain + '/', "", stripped)19 stripped = re.sub('/>', '>', stripped)20 stripped = re.sub('; >', ';">', stripped)21 stripped = re.sub('" alt="', '" alt="', stripped)22 stripped = re.sub('" >', '">', stripped)23 stripped = re.sub('<script src="/', '<script src="', stripped)24 stripped = re.sub('" title="', '" title="', stripped)25 stripped = re.sub('<link rel="canonical" href="http:', '<link rel="canonical" href="index.html', stripped)26 stripped = re.sub('src="/', 'src="', stripped)27 stripped = re.sub('type="text/css" rel="stylesheet" href="/', 'type="text/css" rel="stylesheet" href="', stripped)28 stripped = re.sub('href="/index.html"', 'href="index.html"', stripped)29 #print(stripped)30 pyperclip.copy(stripped)31 #copy = pyperclip.paste()32 return stripped333435class M3U8:36 def download(__url:str, __file:str):37 subprocess.call('ffmpeg -i '+ __url + " " + __file, shell = True)383940class download_media:4142 def video(__last_item: str):43 now = datetime.now()44 time = now.strftime("%m%d%Y%H%M%S")45 __last_item = __last_item.replace("\n\r", "")46 __last_item = __last_item.replace("\n", "")47 M3U8.download(__last_item, "video" + time + ".mp4")48 print(__last_item + " Download done!")4950# #**************** for debugging purpose only ********************51# domain = 'www.ahzixun.cn'52# resp = req.get("http://www.ahzixun.cn")53# print(resp.encoding)54# ncontent = str(resp.content, '<UTF-8>', errors='replace')55# print(webinspector.inspect_html(ncontent))56
...
main.py
Source:main.py
1import urllib.request as ul2from bs4 import BeautifulSoup as bs3import re4import pymongo as pm5#f = open("/home/thamirys/Doutorado/Projects/Coding/Python/Html-extractor/htmltestsignal.txt", "w")6#f.write(pagesignal)7#f.close()8# criando db e collection9database = 'htmlextractor'10collection = 'pages'11def db_collection():12 connect = pm.MongoClient("mongodb://localhost:27017/")13 create_db = connect[database]14 create_collection = create_db[collection]15 return create_collection16# extraindo html da página17def extract_html(link):18 url = ul.Request("https://www.mql5.com" + link, headers={'User-Agent': 'Mozilla/5.0'})19 page = ul.urlopen(url).read().decode("utf-8")20 inspect_html = bs(page,"html.parser")21 return inspect_html22# inserindo html no mongodb23def insert_data_mongo(page_name, link):24 return db_collection().insert_one({25 "page_name": page_name, "html": extract_html(link)26 })27 28# extraindo link para página signals29def signals_link():30 for link in extract_html("").find(id="mainmenuItems").find_all(href=re.compile('signals')):31 signals_link = link['href']32 return signals_link33# extraindo link para página see all signals34def all_signals_link():35 link = signals_link()36 for link in extract_html(link).find_all("div", {"class": "signals-dashboard__see-all"}):37 all_signals_link = link.a['href']38 return all_signals_link39print(all_signals_link())...
test_inspect_html.py
Source:test_inspect_html.py
...6from seleniumbase import BaseCase7class HtmlInspectorTests(BaseCase):8 def test_html_inspector(self):9 self.open("https://xkcd.com/1144/")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!