This commit is contained in:
Daniel Hládek 2023-03-10 16:19:24 +01:00
parent 3d351d5d50
commit 9c3578f5b7

View File

@ -96,9 +96,7 @@ def fetch_pages(link_batch):
htmls.append(trafilatura.fetch_url(link,decode=False)) htmls.append(trafilatura.fetch_url(link,decode=False))
return htmls return htmls
def fetch_front_links(navigation_links): def fetch_rules(base_url):
start_link = navigation_links[0]
known_links =navigation_links[1:]
rules = urllib.robotparser.RobotFileParser() rules = urllib.robotparser.RobotFileParser()
rules.set_url(base_url + '/robots.txt') rules.set_url(base_url + '/robots.txt')
# exceptions happening here # exceptions happening here
@ -107,13 +105,11 @@ def fetch_front_links(navigation_links):
except Exception as exc: except Exception as exc:
LOGGER.error('cannot read robots.txt: %s', exc) LOGGER.error('cannot read robots.txt: %s', exc)
rules = None rules = None
response = trafilatura.fetch_url(homepage, decode=False) return rules
if response is None or response == '':
return None, None, None def fetch_front_links(start_links,rules):
# get redirected URL here? responses = fetch_pages(start_links)
if response.url != homepage:
logging.info('followed redirect: %s', response.url)
homepage = response.url
# decode response # decode response
htmlstring = trafilatura.utils.decode_response(response.data) htmlstring = trafilatura.utils.decode_response(response.data)
# is there a meta-refresh on the page? # is there a meta-refresh on the page?
@ -165,9 +161,6 @@ def index_pages(db,domain,extracted_pages,rules):
doc["paragraph_checksums"] = checksums doc["paragraph_checksums"] = checksums
doc["paragraph_sizes"] = sizes doc["paragraph_sizes"] = sizes
# todo extract links # todo extract links
if "links" in doc:
extracted_links.union(doc["links"])
del doc["links"]
print(doc) print(doc)
contentcol.insert_one(doc) contentcol.insert_one(doc)
# todo extract links # todo extract links
@ -178,14 +171,16 @@ def index_pages(db,domain,extracted_pages,rules):
if len(rl) == 1: if len(rl) == 1:
doc = get_link_doc(rl[0],"redirect") doc = get_link_doc(rl[0],"redirect")
linkcol.replace_one({"url":rl[0]},doc,upsert=True) linkcol.replace_one({"url":rl[0]},doc,upsert=True)
filtered_links = filter_links(extracted_links,rules)
for llink in filtered_links: def extract_links(extracted_pages,domain,rules,status="frontlink"):
doc = get_link_doc(link,"backlink") filtered_links = filter_links(extracted_links,rules)
if courlan.is_external(link,domain): for original_link,final_link,html,doc in extracted_pages:
doc["status"]= "frontlink" doc = get_link_doc(link,status)
elif courlan.is_navigation(link): if courlan.is_external(link,domain):
doc["status"] = "navigation" doc["status"]= "frontlink"
linkcol.insert_one(doc) elif courlan.is_navigation(link):
doc["status"] = "navigation"
linkcol.insert_one(doc)
def get_links(db,domain,status,batch_size=BATCHSIZE): def get_links(db,domain,status,batch_size=BATCHSIZE):
linkcol = db["links"] linkcol = db["links"]