-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
84 lines (63 loc) · 1.99 KB
/
main.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
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
from pyppeteer import launch
from bs4 import BeautifulSoup
import asyncio
import narrate
async def main(keyword):
# Create a browser instance and goto Wikipedia
browser = await launch()
page = await browser.newPage()
await page.goto('https://en.wikipedia.org/wiki/Main_Page')
# Type in search keyword and press enter
await page.type('[id=searchInput]', keyword)
await page.screenshot({'path': 'main_page.png'})
# Press enter and Wait for results to load
await page.keyboard.press('Enter')
await page.waitForNavigation()
await page.screenshot({'path': 'results.png'})
# Get all the links from results
urls = await page.evaluate('''
() => {
const links = document.querySelectorAll('.mw-search-results a')
const urls = Array.from(links).map(link => link.href)
return urls
}
''')
print(urls)
# Goto the first result
await page.goto(urls[0])
await page.screenshot({'path': 'article.png'})
# Get all text in p tags
data = await page.evaluate('''
() => {
const contents = document.querySelectorAll('.mw-parser-output p')
const ps = Array.from(contents).map(para => para.innerHTML)
return ps
}
''')
# Join the result paras with blank space
allData = ' '.join(data)
print(allData)
# Parse it to text from html
soup = BeautifulSoup(allData)
text = soup.get_text().strip()
print(text)
# remove square brackets
finalText = ''
bracket = 0
for i in range(min(len(text), 500)):
if text[i] == '[':
bracket += 1
elif text[i] == ']':
bracket -= 1
elif bracket == 0:
finalText += text[i]
print(finalText)
narrate.narrate(finalText)
await browser.close()
while True:
try:
keyword = input('? ')
asyncio.get_event_loop().run_until_complete(main(keyword))
except EOFError:
print('bye')
break