29 lines
989 B
Python
29 lines
989 B
Python
import requests
|
|
from lxml import html
|
|
word = 'vocabulary'
|
|
# Define the URL
|
|
url = 'https://dictionary.cambridge.org/dictionary/essential-american-english/' + word
|
|
|
|
headers = {
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
}
|
|
response = requests.get(url, headers=headers)
|
|
|
|
# Check if the request was successful
|
|
if response.status_code == 200:
|
|
# Parse the content with lxml
|
|
tree = html.fromstring(response.content)
|
|
|
|
# Use XPath to find the element
|
|
xpath_expression = '/html/body/div[2]/div/div[1]/div[2]/article/div[2]/div[2]/div/span/div/div[3]'
|
|
elements = tree.xpath(xpath_expression)
|
|
|
|
# Check if the element was found and print the text content
|
|
if elements:
|
|
for element in elements:
|
|
print(element.text_content().strip())
|
|
else:
|
|
print("Element not found.")
|
|
else:
|
|
print(f"Failed to retrieve the page. Status code: {response.status_code}")
|