-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
84 lines (71 loc) · 2.44 KB
/
app.py
File metadata and controls
84 lines (71 loc) · 2.44 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import asyncio
from typing import List
from pyppeteer import launch
# import json module
import json
async def get_article_titles(keyword):
search_engine_url="https://duckduckgo.com/"
# launch browser in headless mode
browser = await launch({"headless": False, "args": ["--start-maximized"]})
# create a new page
page = await browser.newPage()
# set page viewport to the largest size
await page.setViewport({"width": 1600, "height": 900})
# navigate to the page
await page.goto(search_engine_url)
# locate the search box
entry_box = await page.querySelector("div")
#.TextInput_userInput__807JK
#.gLFyf
print("====================== {} ======================".format(keyword))
# type keyword in search box
await entry_box.type(keyword)
# wait for search results to load
await page.waitFor(4000)
# press enter key
await page.keyboard.press('Enter')
# wait for search results to load
await page.waitFor(4000)
# list of dictionaries
data = [
{
"search engine":search_engine_url,
"search query":keyword
}
]
# extract the results
results = await page.querySelectorAll("article")
#intialise result index
index=1
for result in results:
# dictionaries
dict = {}
#assigning result index
dict["result"]=index
# extract the title of the result
title = await result.querySelector("h2")
titleContent = await title.getProperty("textContent")
# extract the link of the result
link = await result.querySelector("a:nth-child(2)")
linkContent = await link.getProperty("textContent")
# extract the description of the result
description = await result.querySelector("div:nth-child(3)")
descriptionContent = await description.getProperty("textContent")
#adding key values to the dictionary
dict["title"]=await titleContent.jsonValue()
dict["link"]=await linkContent.jsonValue()
dict["content"]=await descriptionContent.jsonValue()
#adding dictionary to the data list
data.append(dict)
index=index+1
# convert into json
json_object = json.dumps(data, indent=2)
# display
print(json_object)
with open("result.json", "w") as outfile:
outfile.write(json_object)
print("Starting...")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(get_article_titles("coffee"))
print("Finished extracting articles titles")