Skip to content

Instantly share code, notes, and snippets.

@Fitzy1293
Created July 21, 2021 00:14
Show Gist options
  • Save Fitzy1293/0f0f42893445d52961467d1a643697b6 to your computer and use it in GitHub Desktop.
Save Fitzy1293/0f0f42893445d52961467d1a643697b6 to your computer and use it in GitHub Desktop.
import urllib.parse
import urllib.request
BASE_URL = 'http://libgen.rs/search.php'
def libgen_post_request(query, pagenumber):
url_encoded_query = urllib.parse.urlencode({'req':query})
extra_params = '&open=0&res=25&view=simple&phrase=1&column=def&res=100'
libgen_current_page = f'&page={pagenumber}'
full_url = f'{BASE_URL}?{url_encoded_query}{extra_params}{libgen_current_page}'
req = urllib.request.Request(full_url)
with urllib.request.urlopen(req) as response:
the_page_bytes = response.read()
the_page_str = the_page_bytes.decode('utf')
return the_page_str
def libgen_response_grabber(query):
pagenumber = 1
end_pagination_flag = True
while end_pagination_flag:
good_line_flag = False
for line in libgen_post_request(query, pagenumber).splitlines():
if good_line_flag:
print(line)
continue
if line.startswith("<table width=100%><tr><td align='left' width=45%><font color=grey size=1>"):
if '>0 files found<' in line:
end_pagination_flag = False
break
showing_results_str = line.split('|')[1].split('<')[0].strip()
print(showing_results_str)
range_of_results = showing_results_str.replace('showing results from ', '').replace('to', '')
first, last = range_of_results.split()
if int(first) > int(last):
end_pagination_flag = False
else:
print(line)
good_line_flag = True
pagenumber +=1
if __name__ == '__main__':
libgen_response_grabber('the black swan')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment