Loading repository data…
Loading repository data…
rogerchang1108 / repository
In this project, we employ the BeautifulSoup4 package in Python Jupyter Notebook to scrape data from the Cambridge Dictionary website. Subsequently, we refine and organize the scraped data to construct a custom dictionary.
In this project, we employ the BeautifulSoup4 package in Python Jupyter Notebook to scrape data from the Cambridge Dictionary website. By analyzing the HTML structure of the website, we extract relevant URLs using UNIX commands guided by formal language principles. Subsequently, we refine and organize the scraped data to construct a custom dictionary.
Note: Because scraping every URLs may take a LOT of time, we are only working with the first 1000 URLs.
word_dict:
<headword> as the key and an individual dictionary as the value.Individual dictionary:
<headword> as the value.entry_array as the value.entry_array:
entry_array_dict objects.entry_array_dict:
pos_array as the value.sense_body_array as the value.pos_array:
<pos> elements for the current entry.sense_body_array:
def_block_array_dict and phrase_block_array_dict.def_block_array_dict:
<definition> as the value.<定義> (definition in Chinese) as the value.examp_array as the value.examp_array:
examp_arrray_dict objects for the current definition block.examp_arrray_dict:
<sentence> as the value.<例句> (example sentence in Chinese) as the value.phrase_block_array_dict:
<phrase> as the value.phrase_def_block_array as the value.phrase_def_block_array:
phrase_def_block_array_dict objects.phrase_def_block_array_dict:
<phrase definition> as the value.<片語定義> (phrase definition in Chinese) as the value.Import Modules, Initialize URL List, Read URL
Create Dictionary and Loop
Set up HTTP Requests and Header Information
Fetching web data
Confirm the existence of "di-body" to determine if the URL request was successful.
Based on different word directories, set different "headword" classes to fetch actual headwords/phrases.
First, confirm the structure; some web pages may not necessarily have an "entry-body". If absent, use the previous "di-body" as the index for subsequent retrieval.
Within the "entry", fetch the part of speech ("pos"), and if absent, write "N/A".
Within the "entry", follow the webpage structure to fetch definitions and related phrase items under "sense-body".
Within the "sense-body", follow the webpage structure to fetch English and Chinese definitions under "def-block". If absent, write "N/A".
Within "def-block", follow the webpage structure to fetch English and Chinese example sentences under "examp" and "dexamp". If absent, write "N/A".
Check if "phrase-block" exists within the "sense-body".
If present, fetch the "phrase". Within "phrase-block", follow the webpage structure to fetch English and Chinese definitions of the phrase under "def-block". If there's no Chinese definition, write "N/A".
If absent, skip the "phrase" section.
Save as JSON file: The JSON string is generated using json.dumps and then written to a file.
Retry Mechanism: If a request fails (e.g., due to a network issue), the program will attempt a maximum of 3 retries.
Handling Request Failures: If data cannot be retrieved even after 3 retries, the code will add the URL as the headword and append the headword with the message "URL request fail" to the word_dict.