-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsync_get_url.py
More file actions
35 lines (26 loc) · 1016 Bytes
/
Async_get_url.py
File metadata and controls
35 lines (26 loc) · 1016 Bytes
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
from bs4 import BeautifulSoup
import grequests
from fake_useragent import UserAgent
urls = ['https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5396460/',
]
def main():
async_list = []
# Bolt: Instantiate UserAgent once outside the loop.
# UserAgent() parses a large JSON file and takes ~0.6s per call,
# so doing it in the loop is a major performance bottleneck.
ua = UserAgent()
for site in urls:
action_item = grequests.get(
site, headers={"User-Agent": ua.random}, hooks={'response': handleresponse})
async_list.append(action_item)
grequests.map(async_list)
def handleresponse(response, **kwargs):
articles = BeautifulSoup(response.text, 'html.parser')
heading_title = articles.find_all(
'h1', class_='content-title')[0].get_text()
print(heading_title + "\n")
date_pubed = articles.find(
"meta", attrs={'name': 'citation_publication_date'})['content']
print(date_pubed)
if __name__ == '__main__':
main()