-
Notifications
You must be signed in to change notification settings - Fork 374
/
expl_qwant.py
28 lines (22 loc) · 756 Bytes
/
expl_qwant.py
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
"""Example usage of MechanicalSoup to get the results from the Qwant
search engine.
"""
import re
import urllib.parse
import mechanicalsoup
# Connect to duckduckgo
browser = mechanicalsoup.StatefulBrowser(user_agent='MechanicalSoup')
browser.open("https://lite.qwant.com/")
# Fill-in the search form
browser.select_form('#search-form')
browser["q"] = "MechanicalSoup"
browser.submit_selected()
# Display the results
for link in browser.page.select('.result a'):
# Qwant shows redirection links, not the actual URL, so extract
# the actual URL from the redirect link:
href = link.attrs['href']
m = re.match(r"^/redirect/[^/]*/(.*)\?.*$", href)
if m:
href = urllib.parse.unquote(m.group(1))
print(link.text, '->', href)