-
Notifications
You must be signed in to change notification settings - Fork 4
/
scraper_functions.py
1201 lines (922 loc) · 41.1 KB
/
scraper_functions.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# # Please install the required Python libraries
# !conda install pandas
# !conda install numpy
# !conda install jupyter
# !conda install requests
# !conda install -c anaconda beautifulsoup4
# !conda install soupsieve -y
# !conda install lxml -y
# !pip install selenium
# !pip install webdriver_manager
# !pip3 install --upgrade pandas # library for manipulating structured data
# !pip3 install --upgrade numpy # library for fundamentals of array computing
# !pip3 install --upgrade requests # library for making request for the static websites
# !pip3 install --upgrade soupsieve # library to support css selector in beautifulsoup
# !pip3 install --upgrade beautifulsoup4 # a parser that balances between efficiency and leniency
# !pip3 install --upgrade --user lxml # a more efficient parser
# !pip3 install --upgrade html5lib # a parser that acts like a browser, most lenient
# !pip install --upgrade webdriver-manager # library that helps user manage the installation and usage of web drivers # pip3 not supported
# !pip3 install selenium-wire==3.0.6 # library for automating web browser interaction, extended to inspect requests
# Basic libraries
import os
import re
import json
import time
import random
import inspect
import numpy as np
import pandas as pd
from glob import glob
pd.options.display.max_colwidth = 200
# Web scraping related libraries
import requests
import bs4
from selenium import webdriver # seleniumwire # $$$
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Other libraries
from collections import OrderedDict
from collections.abc import Iterable
from IPython.display import clear_output
##################################################################################################
def initialize_driver(mode = 'fast', implicit_wait = 10):
"""Initialize and return the web driver.
Parameters
----------
mode: string (optional, default = 'fast')
Loading strategy for the web driver, "fast" or "complete".
implicit_wait: int (optional, default = 10) # seconds
How many seconds to wait before the driver throw a "No Such Element Exception".
Returns
----------
seleniumwire.webdriver.browser.Chrome
"""
caps = DesiredCapabilities().CHROME
caps['pageLoadStrategy'] = 'eager' if mode == 'fast' else 'normal' if mode == 'complete' else 'none'
driver = webdriver.Chrome(ChromeDriverManager().install(), desired_capabilities = caps)
# driver = webdriver.Chrome(executable_path = '/Users/timsmac/chromedriver', desired_capabilities = caps) # $$$
driver.implicitly_wait(implicit_wait)
return driver
##################################################################################################
user_name = input('Hi, what is your name? ')
print('\nNice to meet you, '+user_name+'! Thanks for teaching me scrape...\n')
time.sleep(1)
driver_type = input('Do you need to scrape dynamically loaded website?\n\n(Note choosing "Yes" here will require web-driver installation and scraping progress will be slower. You can also choose to initialize dynamic website scraper later.)\n\nYour choice: [Yes / No]\n\n')
if driver_type.lower()[0] == 'y':
clear_output()
print('\n\nPlease choose the loading strategy for your dynamic scraper:\n')
time.sleep(1)
dynamic_driver_mode = input('Fast mode: proceed once the basic structure of the web page is loaded.\n\nComplete mode: proceed only after all resources on the page are fully loaded.\n\nYour choice: [Fast / Complete]\n\n')
print('\n\nOkay, initializing ', end='')
for i in range(4):
time.sleep(0.4)
print('.',end='')
driver = initialize_driver(mode = dynamic_driver_mode.lower())
clear_output()
print('\nDone, the environment is ready. Now Teach Me Scrape!\n')
else:
print('\n\nOkay, initializing ', end='')
for i in range(4):
time.sleep(0.4)
print('.',end='')
driver = None
clear_output()
print('\nDone, the environment is ready. Now Teach Me Scrape!\n')
##################################################################################################
def ordered_remove_duplicates(li):
"""Remove duplicates and arrange the unique elements by the order of their first occurences.
Parameters
----------
li: list
Returns
----------
list
"""
return list(OrderedDict.fromkeys(li))
def remove_blank_element_in_list(li):
"""Return a cleaned version of the list with all blank elements removed.
Parameters
----------
li: list
Returns
----------
list
"""
return [element for element in li if element.strip()!='']
def flatten_list(l):
"""Flatten a list of lists to a one-layer list (elements are in original order). Note this is NOT recursive, meaning multi-layered list of lists cannot be converted into a single-layered list in one transformation.
Parameters
----------
l: list
Returns
----------
list
"""
return [item for sublist in l for item in sublist]
def robust_flatten_list(li):
if not any([isinstance(x,(list,tuple)) for x in li]):
return li # no need to be flatten, no element of the list is list-or-tuple type
new_li = []
for x in li:
if not isinstance(x,(list,tuple)): # if this element is not a list-or-tuple type, make it temporarily wrapped in a singleton list
new_li.append( [x] )
else:
new_li.append( x )
return flatten_list(new_li)
def deep_flatten_list(li):
if not any([isinstance(x,(list,tuple)) for x in li]):
return li # no need to be flatten, no element of the list is list-or-tuple type
return deep_flatten_list( robust_flatten_list(li) )
def is_iterable(obj):
"""Check if the passed object is iterable.
Parameters
----------
obj: object
Returns
----------
boolean
"""
return isinstance(obj, Iterable)
##################################################################################################
# These functions help us understand the variables that exist in the environment
# which is useful for creating natural language interface for data analysis
def get_local_variables(ignore_underscore = True):
"""Get the name and definition of the local variables.
Parameters
----------
ignore_underscore : boolean (optional, default = True)
Whether or not the variables starting with "_" need to be filtered out.
Returns
----------
dictionary
A mapping between name and definition of the local variables.
"""
callers_local_vars = dict(inspect.currentframe().f_back.f_locals.items())
if ignore_underscore:
var_keys = list(callers_local_vars.keys())
for key in var_keys:
if key.startswith('_'):
del callers_local_vars[key]
return callers_local_vars
def get_global_variables(ignore_underscore = True):
"""Get the name and definition of the global variables.
Parameters
----------
ignore_underscore : boolean (optional, default = True)
Whether or not the variables starting with "_" need to be filtered out.
Returns
----------
dictionary
A mapping between name and definition of the global variables.
"""
callers_global_vars = dict(inspect.currentframe().f_back.f_globals.items())
if ignore_underscore:
var_keys = list(callers_global_vars.keys())
for key in var_keys:
if key.startswith('_'):
del callers_global_vars[key]
return callers_global_vars
def retrieve_name(var):
"""Retrieve the name of the variable. # Reference https://stackoverflow.com/a/40536047.
Parameters
----------
var: object
Variable to get the name of.
Returns
----------
string
Name of the variable passed.
"""
for fi in reversed(inspect.stack()):
names = [var_name for var_name, var_val in fi.frame.f_locals.items() if var_val is var]
if len(names) > 0:
return names[0]
def get_attributes(obj, ignore_underscore = True):
"""Get a list of valid attributes of the object.
Parameters
----------
ignore_underscore : boolean (optional, default = True)
Whether or not the variables starting with "_" need to be filtered out.
Returns
----------
list
A list of valid attributes of the object.
"""
return [x for x in dir(obj) if not x.startswith('_')]
def print_attributes_and_values(obj, ignore_underscore = True):
"""Print the valid attributes of the object and their corresponding values.
Parameters
----------
ignore_underscore : boolean (optional, default = True)
Whether or not the variables starting with "_" need to be filtered out.
Returns
----------
None
"""
obj_name = retrieve_name(obj)
attributes = get_attributes(obj, ignore_underscore = ignore_underscore)
for attr in attributes:
obj_attr_string = obj_name+'.'+attr
print(obj_attr_string)
print(' '*4 + str(eval(obj_attr_string))[:60])
print('-'*70)
##################################################################################################
def is_readable_content(content):
"""Return whether the content passed is a readable content like Tag or NavigableString; not CData, Comment, Declaration, Doctype, ProcessingInstruction, ResultSet, Script, Stylesheet, XMLFormatter.
Parameters
----------
content: bs4.element
An BS4 element from the parsed tree.
Returns
----------
boolean
"""
# Types that are instances of NavigableString: CData, Comment, Declaration, Doctype, PreformattedString, ProcessingInstruction, ResultSet, Script, Stylesheet, TemplateString, XMLFormatter
# Types in the group above that are not String: CData, Comment, Declaration, Doctype, ProcessingInstruction, ResultSet, Script, Stylesheet, XMLFormatter
return isinstance(content, (bs4.element.Tag, bs4.element.NavigableString)) and not isinstance(content, (bs4.element.CData, bs4.element.Comment, bs4.element.Declaration, bs4.element.Doctype, bs4.element.ProcessingInstruction, bs4.element.ResultSet, bs4.element.Script, bs4.element.Stylesheet, bs4.element.XMLFormatter))
def get_contents(element):
"""Return a list of non-empty and readable contents/children of the element.
Parameters
----------
content: bs4.element
An BS4 element from the parsed tree.
Returns
----------
list of bs4.element
"""
return [content for content in element.contents if str(content).strip()!='' and is_readable_content(content)]
def get_contents_names(element):
"""Return the list of names of the non-empty and readable contents/children of the element.
Parameters
----------
content: bs4.element
An BS4 element from the parsed tree.
Returns
----------
list of string
"""
return [content.name for content in get_contents(element)]
##################################################################################################
def get_response(url, verbose = True, driver = driver ):
"""Get the response of the HTTP GET request for the target url.
Parameters
----------
url: string
The url to the website that needs to be scraped.
verbose: boolean (optional, default = True)
Whether or not [Success] message should be printed.
driver: seleniumwire.webdriver.browser.Chrome (optional, default use global variable driver)
The web browser driver with which to get the page source of dynamic website.
Returns
----------
response object or string
"""
# Static mode when driver is None
if driver is None:
headers_list = [{'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Referer': 'https://www.google.com/', 'DNT': '1', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1'}, {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Referer': 'https://www.google.com/', 'DNT': '1', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1'}, {'Connection': 'keep-alive', 'DNT': '1', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Dest': 'document', 'Referer': 'https://www.google.com/'}, {'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-User': '?1', 'Sec-Fetch-Dest': 'document', 'Referer': 'https://www.google.com/'}] # Reference: https://www.scrapehero.com/how-to-fake-and-rotate-user-agents-using-python-3
try:
headers = random.choice(headers_list)
response = requests.get(url, headers = headers)
response.raise_for_status() # Raise Exception when response was not successful
except requests.exceptions.HTTPError as http_err:
print('[Error] HTTP error occurred: '+str(http_err))
return requests.models.Response() # Return empty response
except Exception as err:
print('[Error] Other error occurred: '+str(err))
return requests.models.Response() # Return empty response
else:
if verbose:
print('[Success] The website at "'+url+'" is collected successfully.')
return response
# Dynamic mode when driver is provided
else:
if not is_driver_at_url(driver, url):
go_to_page(driver, url)
scroll_to_bottom(driver)
return get_page_source(driver)
def get_soup(response, default_parser = 'lxml'):
"""Get the beautiful soup object of the response object or filepath or html string.
Parameters
----------
response: requests.models.Response, string
The response object or filepath or html string.
default_parser: string (optional, default = lxml)
Which parser to use when parsing the response.
Returns
----------
list of response object
"""
if isinstance(response, requests.models.Response):
soup = bs4.BeautifulSoup(response.content, default_parser)
else:
try:
soup = bs4.BeautifulSoup(response, default_parser)
except Exception as err:
print('[Error] The response object you provided cannot be turned into beautiful soup object: '+str(err))
return soup
def save_html(html_object , path = '', url = ''): # $$$
"""Save the response or soup object as a HTML file at the path provided.
Parameters
----------
html_object: requests.models.Response, bs4.BeautifulSoup
The response or soup object.
path: string (optional, default = ./TEMP.html)
The path at which the HTML file will be saved.
Returns
----------
None
"""
if path == '':
if url != '':
path = './'+re.sub('^https?://','',url).replace('/','_').replace('.','-')+'.html'
else:
path = './TEMP.html'
if isinstance(html_object, requests.models.Response):
html_text = html_object.text
elif isinstance(html_object, (bs4.BeautifulSoup,bs4.element.Tag)):
html_text = str(html_object.prettify())
else:
html_text = str(html_object)
try:
with open(path,'w') as f:
f.write(html_text)
print('[Success] The HTML file is saved succesfully.')
except Exception as err:
print('[Error] The response object you provided cannot be turned into beautiful soup object: '+str(err))
def get_response_and_save_html(url, driver = driver, path = ''):
"""Get the response of the website and save it as an HTML.
Parameters
----------
url: string
The url to the website that needs to be scraped.
driver: seleniumwire.webdriver.browser.Chrome (optional, default use global variable driver)
The web browser driver with which to get the page source of dynamic website.
path: string (optional, default = ./TEMP.html)
The path at which the HTML file will be saved.
Returns
----------
None
"""
response = get_response(url, driver = driver)
save_html(response.text, url, path = path)
##################################################################################################
def get_self_index(element):
"""Return the index of the element among its siblings of the same type.
Parameters
----------
element: bs4.element
An BS4 element from the parsed tree.
Returns
----------
int
"""
self_type = element.name
previous_siblings_of_all_types = list(element.previous_siblings)
previous_siblings_of_same_type = [element for element in previous_siblings_of_all_types if element.name == self_type]
return len(previous_siblings_of_same_type) + 1 # css selector starts indexing with 1 instead of 0
def describe_part_of_css_selector(element):
"""Construct part of the css selector path.
# Reference: https://stackoverflow.com/a/32263260 (basic structure inspiration)
# Reference: https://csswizardry.com/2012/05/keep-your-css-selectors-short (tips to improve efficiency)
Parameters
----------
element: bs4.element
An BS4 element from the parsed tree.
Returns
----------
string
"""
enough_to_be_unique = False
element_type = element.name
element_attrs = element.attrs
element_attrs_string = ''
for k,v in element_attrs.items():
if k == 'id' and str(element_attrs[k])!='':
element_attrs_string += '#' + element_attrs[k]
enough_to_be_unique = True
break
elif k == 'class' and len(element_attrs[k])>0:
element_attrs_string += '.'+'.'.join(element_attrs[k])
element_part = element_type + element_attrs_string
if not enough_to_be_unique:
length = get_self_index(element)
if (length) > 1:
element_part = '%s:nth-of-type(%s)' % (element_part, length)
return element_part
def get_css_selector_path(node):
"""Construct the whole css selector path to a certain element.
Parameters
----------
node: bs4.element
An BS4 element from the parsed tree.
Returns
----------
string
"""
path = [describe_part_of_css_selector(node)]
for parent in node.parents:
if parent.name == 'html' :
break
path.insert(0, describe_part_of_css_selector(parent))
return ' > '.join(path)
def elevate_css_selector_path(path):
"""Get the css selector path to the element that is one level above the current element.
Parameters
----------
path: string
The css selector path to an BS4 element from the parsed tree.
Returns
----------
string
"""
return '>'.join(path.split('>')[:-1]).strip() if '>' in path else path
def go_up_multiple_level(orig_path, go_up):
"""Get the css selector path to the element multiple levels up.
Parameters
----------
orig_path: string
The css selector path to the source element.
go_up: int
The number of levels to go up.
Returns
----------
string
"""
path = orig_path[:]
for i in range(go_up):
path = elevate_css_selector_path(path)
return path
def go_up_till_is_tag(element):
"""Return the nearest Tag element, if not itself, return its parent if it is a Tag element.
Parameters
----------
element: bs4.element
An BS4 element from the parsed tree.
Returns
----------
bs4.element.Tag
"""
if isinstance(element, bs4.element.NavigableString):
return element.parent
if isinstance(element, bs4.element.Tag):
return element
else:
print('[Error] Element is still not Tag after getting the parent.')
return None
##################################################################################################
def get_directly_related_link(element):
"""Extract the link directly related to the element.
Parameters
----------
element: bs4.element
Returns
----------
string
"""
count = 0
while element.name != 'a' and count < 5:
element = element.parent
if element is None:
return ''
count += 1
if element.name != 'a':
return ''
else:
return element.get('href',default='')
def get_indirectly_related_links(element):
"""Extract the links indirectly related to the element (i.e. belonging to the sibling elements).
Parameters
----------
element: bs4.element
Returns
----------
list of string
"""
return remove_blank_element_in_list([link.get('href',default='') for link in element.parent.find_all('a')])
def get_related_link(element):
"""Extract the link directly related to the element, if none is found, get indirectly related links.
Parameters
----------
element: bs4.element
Returns
----------
string or list of string
"""
link = get_directly_related_link(element)
if link != '':
return link
else:
links = get_indirectly_related_links(element)
if len(links) == 1 and links[0].strip() != '':
return links[0]
else:
return links
##################################################################################################
def extract_text(element):
"""Extract the textual content of an element.
Parameters
----------
element: bs4.element
Returns
----------
string
"""
return element.getText(separator=u'\n').strip()
def get_longest_separator(text):
"""Return the longest separator (formed by multiple newline) in the text.
Parameters
----------
text: string
Returns
----------
string
"""
if isinstance(text, str) and '\n' in text:
return max(re.findall(r'\n+', text, re.DOTALL), key=lambda x: len(x))
else:
return ''
def recursive_split(text):
"""Return a multi-layer list of lists resulting from a recursive split of the text (split by longer separator first).
Parameters
----------
text: String
A piece of text that contains separators of different lengths.
Returns
----------
list (of lists)
"""
longest_separator = get_longest_separator(text)
if longest_separator == '':
return text
else:
return [recursive_split(part) for part in remove_blank_element_in_list(text.split(longest_separator))]
##################################################################################################
def extract_contents(soup, path, verbose = True):
"""Extract and return the texts and links with the target path in the parsed tree.
Parameters
----------
soup: bs4.soup
The parsed tree of the response.
path: string
The css selector path to the target elements.
verbose: boolean (optional, default = True)
Whether or not to print the process message.
Returns
----------
pd.DataFrame
"""
if soup is None:
return None
if isinstance(soup, pd.DataFrame):
return soup
if verbose:
print('\nExtracting contents ...\n')
if path.startswith('HEADER:'):
try:
tables = pd.read_html(str(soup))
target_table = [table for table in tables if str(tuple(table.columns.tolist())) == path.replace('HEADER:','')][0]
return target_table
except:
return []
target_elements = soup.select(path)
data = pd.DataFrame([(recursive_split(extract_text(target_element)), get_related_link(target_element)) for target_element in target_elements], columns = ['text','url'])
return data
##################################################################################################
def get_unique_sample_element(soup, target_phrase = '', context_radius = 40):
"""Find and return an element based on the html structure and a target phrase, solicit additional information from user through input questions if needed.
Parameters
----------
soup: bs4.soup
The parsed tree of the response.
target_phrase: string (optional, if not provided, the function will ask user to input)
The phrase used to find the sample element.
context_radius: int (optional, default = 40)
How many characters to display to help user choose recurring phrases based on their contexts.
Returns
----------
bs4.element.Tag
"""
target_phrase = target_phrase.lower()
matched_elements = soup.find_all(text = re.compile(target_phrase,re.IGNORECASE))
attempt_count = 1
while len(matched_elements)!=1:
################################################################
# Situation where matched elements have the same textual content
if len(set([str(matched_element) for matched_element in matched_elements]))==1:
last_index = -1
phrases_in_context = []
whole_page_text = re.sub('\s+',' ',soup.text).lower()
if whole_page_text.count(target_phrase) == len(matched_elements):
for i in range(whole_page_text.count(target_phrase)):
current_index = whole_page_text.index(target_phrase,last_index+1)
phrases_in_context.append(whole_page_text[current_index-context_radius:current_index]+'\\\\ '+whole_page_text[current_index:current_index+len(target_phrase)]+' //'+whole_page_text[current_index+len(target_phrase):current_index+len(target_phrase)+context_radius])
last_index = current_index
if len(set(phrases_in_context))==1:
print('[Error] There are '+str(len(phrases_in_context))+' occurences of the same target phrase on the page that have very similar contexts.\nPlease use the browser inspector tool to copy the "selector" or "Selector Path".\n')
return None
else:
numbered_contexts = ''
for i in range(len(phrases_in_context)):
numbered_contexts += 'Choice '+str(i+1)+': '+phrases_in_context[i] + '\n'
print('There are '+str(len(phrases_in_context))+' occurences of the same target phrase on the page,\nplease choose one based on their contexts:\n\n' + numbered_contexts + '\n')
which_one = 0
while which_one-1 not in range(len(phrases_in_context)):
which_one = input('Which choice is the element you that want to scrape: [1, 2, 3, ...]\n')
try:
which_one = int(which_one)
except:
which_one = 0
matched_elements = [matched_elements[which_one-1]]
else:
print('[Error] The number of matched elements and the number of target phrase occurences are not the same.\nPlease use the browser inspector tool to copy the "selector" or "Selector Path".\n')
return None
###########################################################
if len(matched_elements) > 0 and len(matched_elements) < 5:
# List numbered choices
numbered_choices = ''
for i in range(len(matched_elements)):
numbered_choices += '\tChoice '+str(i+1)+': '+str(matched_elements[i])[:80]+ '\n'
print('\nThere are '+str(len(matched_elements))+' matched elements given your last input. They are:\n'+numbered_choices)
# Choose one
which_one = 0
while which_one-1 not in range(len(matched_elements)):
which_one = input('Which choice is the element you that want to scrape: [1, 2, 3, ...]\n')
try:
which_one = int(which_one)
except:
which_one = 0
matched_elements = [matched_elements[which_one-1]]
else:
if len(matched_elements) > 5:
print('\nThere are '+str(len(matched_elements))+' matched elements given your last input. They are:\n\n\t'+'\n\t'.join([str(matched_element)[:80] for matched_element in matched_elements[:10]])+'\n\nPlease be more specific in your target phrase.\n')
if len(matched_elements) == 0:
print('\nNo match was found, please check for typos in the target phrase (case insensitive) or check if the website is fully collected.')
# Search again
target_phrase = input('What is the displayed text for one of the elements you want to scrape: '+('(Type "QUIT" to stop)' if attempt_count>3 else '')+'\n')
if target_phrase == 'QUIT':
print('\n[Error] It is likely that the website is not fully collected.\n Please try this command: get_response_and_save_html(PUT_IN_YOUR_URL)\n A HTML file will be created in your local folder, open it with a browser.\n If you cannot see what you want to find on the page, please switch to dynamic scraping method.\n')
return None
matched_elements = soup.find_all(text = re.compile(target_phrase,re.IGNORECASE))
# Increment attempt count
attempt_count += 1
# Match is found by this point
sample_element = matched_elements[0]
sample_element = go_up_till_is_tag(sample_element)
print('\nUnique match is found:\n'+str(sample_element)[:100]+ (' ......' if len(str(sample_element))>100 else '') +'\n\n')
# If the sample element is script tag, handle it differently
if sample_element.name == 'script':
matched_lines = [line for line in sample_element.prettify().split('\n') if target_phrase in line.lower()]
try:
assert(len(matched_lines)==1)
matched_line = matched_lines[0].strip().strip(';')
matched_data = matched_line.split('=',maxsplit=1)[1].strip()
data = pd.DataFrame(json.loads(matched_data))
return data
except:
print('[Error] There are multiple occurences of the target phrase in the JS script.\nPlease use another more unique target phrase or inspect the page source for the data in JS script.\n')
return None
return sample_element
##################################################################################################
def scrape_what_from_where(target_phrase, url, driver = driver, go_up = 0):
"""Get the contents that are similar to the element with phrase "what" in the website "where".
Parameters
----------
target_phrase: string
The displayed text of one of the elements you want to scrape.
url: string
The url of the website you want to scrape.
go_up: int (optional, default = 0)
How many levels to go up in order to get the amount of contents you want.
driver: seleniumwire.webdriver.browser.Chrome (optional, default use global variable driver)
The web browser driver with which to get the page source of dynamic website.
Returns
----------
pd.DataFrame
"""
# Get response
response = get_response(url, driver = driver)
# Get parse tree
soup = get_soup(response)
# Check if the data is in a table, if so, directly return the table
try:
tables = pd.read_html(str(soup))
except:
tables = []
if len(tables)>0 and (len(set([tuple(table.columns.tolist()) for table in tables])) == len(tables)):
tables_containing_target_phrase = [table for table in tables if target_phrase in str(table)]
tables_containing_target_phrase = sorted(tables_containing_target_phrase, key=lambda t: len(str(t)))
if len(tables_containing_target_phrase)>0:
while len(tables_containing_target_phrase)>0:
print('\nThere are '+str(len(tables_containing_target_phrase))+' tables with the target phrase:\n')
target_table = tables_containing_target_phrase[0]
print(target_table)
is_right_table = input('\nIs this table what you want to scrape? [Yes/No]\n')
if is_right_table.lower()[0] == 'y':
right_header = tuple(target_table.columns.tolist())
print('\nThe right header is:\n\t'+str(right_header))
return target_table, 'HEADER:'+str(right_header)
else:
tables_containing_target_phrase.pop(0)
if len(tables_containing_target_phrase)==0:
print('\nThe target data is not one of the tables, moving on to other html elements.\n')
# Pinpoint the sample element through dialogue
sample_element = get_unique_sample_element(soup, target_phrase)
if sample_element is None:
return None, ''
if isinstance(sample_element, pd.DataFrame):
print('[Success] Data is in the JS script and now extracted as a DataFrame into the variable "soup".\n')
return sample_element, ''
# Build the css selector path to the sample element
sample_path = get_css_selector_path(sample_element)
# Go up the parse tree if needed:
path = go_up_multiple_level(sample_path, go_up = go_up)
# Extract content
data = extract_contents(soup, path)
# If data is extracted from html path instead of from json, print the path for future use
if path != '':
print('\n[Success] The selector path used to extract contents is:\n\n\t'+path+'\n')
return data, path
##################################################################################################
def create_page_url_list(template_url, start_index, end_index, unique_first_url = None):
"""Generate a list of urls to scrape from.
Parameters
----------
template_url: string
The url template with placeholder "NUMBER" that will be replaced by index number.
start_index: int
The first index to be plugged into the template.
end_index: int
The last index to be plugged into the template.
unique_first_url: string (optional, default = None)
If the first web page in the pagination process has a different format compared the one after, provide it here.
Returns
----------
list of string
"""
page_url_list = []
if unique_first_url is not None:
page_url_list.append(unique_first_url)
for i in range(start_index,end_index+1):
page_url_list.append(template_url.replace('NUMBER',str(i)))
return page_url_list
def get_base_url(url):
"""Get the base url from a url path.
Parameters
----------