Files
videoreg.ru/scripts/rebuild-v2/fetch-semrush-referrers.py
T

18 lines
1.9 KiB
Python

from pathlib import Path
import json,sys,hashlib,concurrent.futures
from bs4 import BeautifulSoup
from urllib.parse import urljoin,urlsplit
sys.path.insert(0,str(Path(__file__).parent));from fetch import fetch
P=Path(__file__).resolve().parents[2];D=P/'docs/rebuild-v2';urls=['https://2126.ru/forum/index.php?topic=27783.0','http://mtsc.khpi.edu.ua/article/view/47926','http://www.nissan4x4-club.ru/Forum/NISSAN_4x4/index.php?id=353180','https://forum.zarulem.ws/?showtopic=170164','http://terranoirk.ru/forums/viewtopic.php?p=28099'];out=[]
def work(url):
name='semrush-referrer-'+hashlib.sha256(url.encode()).hexdigest()[:14]+'.html';rec=fetch(url,name,25);row=dict(url=url,request=rec,purpose='Actual parent-observed Lost andVital backlink lead, not original videoreg body',videoregLinks=[],relatedDocumentLeads=[])
if rec['httpStatus']=='200':
s=BeautifulSoup((P/rec['raw']).read_bytes(),'html.parser');row['title']=s.title.get_text(' ',strip=True) if s.title else None
for tag in s.find_all('a',href=True):
target=urljoin(url,tag['href']);context=' '.join(tag.parent.get_text(' ',strip=True).split())[:600]
if 'videoreg.ru' in target:row['videoregLinks'].append(dict(target=target,anchor=tag.get_text(' ',strip=True),context=context))
elif any(x in target.lower() for x in ['.pdf','.zip','svk','videoreg','svk-lab']):row['relatedDocumentLeads'].append(dict(target=target,anchor=tag.get_text(' ',strip=True),context=context))
return row
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as ex:out=list(ex.map(work,urls))
(D/'semrush/referrer-verification.json').write_text(json.dumps(dict(scope='5 explicitly supplied public referring URLs out of coordinator-observed18Lost-andVitalrows; remaining13notinvented',rows=out),ensure_ascii=False,indent=2)+'\n');print([(x['url'],x['request']['httpStatus'],len(x['videoregLinks']),len(x['relatedDocumentLeads'])) for x in out])