FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
My-Python-Code/Parallel-Scraper.py at main · aminzayer/My-Python-Code · GitHub
aminzayer
/
My-Python-Code
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
My-Python-Code
/
Parallel-Scraper.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (36 loc) · 1.65 KB
Breadcrumbs
My-Python-Code
/
Parallel-Scraper.py
Copy path
File metadata and controls
48 lines (36 loc) · 1.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
bs4
import
BeautifulSoup
from
fake_useragent
import
UserAgent
import
grequests
my_links
=
[
'https://pubmed.ncbi.nlm.nih.gov/31456179/'
,
'https://pubmed.ncbi.nlm.nih.gov/20521754/'
,
'https://pubmed.ncbi.nlm.nih.gov/29284222/'
,
'https://pubmed.ncbi.nlm.nih.gov/15894099/'
,]
# Get the date from the text
def
Parallel_Fetching_Link_Data
(
urls
):
# Pubmed link data structure
LinksData
=
{
'Error'
:
'OK'
,
'title'
:
''
,
'abstract'
:
''
,
'PostedDate'
:
''
}
Url_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
url
in
urls
:
url_data_response
=
grequests
.
get
(
url
,
headers
=
{
"User-Agent"
:
ua
.
random
},
hooks
=
{
'response'
:
Handel_Response_Fetcheing
})
Url_list
.
append
(
url_data_response
)
grequests
.
map
(
Url_list
,
exception_handler
=
Exception_Handel_Response_Fetcheing
,
size
=
100
)
def
Handel_Response_Fetcheing
(
response
,
**
kwargs
):
articles
=
BeautifulSoup
(
response
.
text
,
'html.parser'
)
heading_title
=
articles
.
find_all
(
'h1'
)[
0
].
get_text
()
heading_title
=
" "
.
join
(
heading_title
.
split
())
abstract
=
articles
.
find_all
(
'div'
,
class_
=
'abstract-content selected'
)[
0
].
get_text
().
replace
(
'
\t
'
,
''
)
abstract
=
" "
.
join
(
abstract
.
split
())
print
(
"
\n
"
)
print
(
"URL:"
,
response
.
url
)
print
(
"Title:"
,
heading_title
)
print
(
"Abstract:"
,
abstract
)
def
Exception_Handel_Response_Fetcheing
(
request
,
exception
):
print
(
"
\n
\n
"
,
request
.
url
,
"
\n
Error: "
,
exception
)
Parallel_Fetching_Link_Data
(
my_links
)
Back
|
FazBrowse Home
|
New Git URL