From 1cea8f5ff2f44ffe25b391ff5666f690a0ad742b Mon Sep 17 00:00:00 2001 From: Karen Zeng Date: Wed, 26 Apr 2023 19:14:54 -0400 Subject: [PATCH] final project update --- fp1.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fp2.py | 10 ++++++++++ fp2_2.py | 32 ++++++++++++++++++++++++++++++++ fp2_3.py | 32 ++++++++++++++++++++++++++++++++ fp2_4.py | 36 ++++++++++++++++++++++++++++++++++++ hw12.py | 7 +++++++ 6 files changed, 170 insertions(+) create mode 100644 fp1.py create mode 100644 fp2.py create mode 100644 fp2_2.py create mode 100644 fp2_3.py create mode 100644 fp2_4.py create mode 100644 hw12.py diff --git a/fp1.py b/fp1.py new file mode 100644 index 0000000..7c34e0c --- /dev/null +++ b/fp1.py @@ -0,0 +1,53 @@ +from google.cloud import pubsub_v1 +from google.oauth2 import service_account +import os + +os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "C:/Users/Karen/AppData/Roaming/gcloud/application_default_credentials.json" +project_id = "fp-20230425" + +''' +topic_name = "fp-topic-Karen" +subscription_name = "fp-subscription-Karen" +subscriber = pubsub_v1.SubscriberClient() +subscription_path = f"projects/{project_id}/subscriptions/{subscription_name}" +subscriber.delete_subscription(request={"subscription": subscription_path}) +print("Subscription deleted: {}".format(subscription_path)) + +# Delete the topic +publisher = pubsub_v1.PublisherClient() +topic_path = publisher.topic_path(project_id, topic_name) +publisher.delete_topic(request={"topic": topic_path}) +print(f"Topic '{topic_name}' deleted.") + +subscription_name = "fp-subscription-Karen" +subscription_path = f"projects/{project_id}/subscriptions/{subscription_name}" + + +''' +# Create a pubsub topic +publisher = pubsub_v1.PublisherClient() +topic_name = "fp-topic-Karen" +topic_path = publisher.topic_path(project_id, topic_name) +topic = publisher.create_topic(request={"name": topic_path}) + + +# Create a Pub/Sub subscription +subscriber = pubsub_v1.SubscriberClient() +subscription_name = "fp-subscription-Karen" +subscription_path = subscriber.subscription_path(project_id, subscription_name) +subscription = subscriber.create_subscription( + name=subscription_path, topic=topic_path +) + +# Publish a message to the topic +message = b"Hello!" +publisher.publish(topic_path, data=message) +print("Message published.") + + +# Pull the message from the subscription +response = subscriber.pull(subscription=subscription_path, max_messages=1) +for received_message in response.received_messages: + print("Received message: {}".format(received_message.message.data)) + # Acknowledge the message to mark it as processed + subscriber.acknowledge(subscription_path, [received_message.ack_id]) diff --git a/fp2.py b/fp2.py new file mode 100644 index 0000000..6975742 --- /dev/null +++ b/fp2.py @@ -0,0 +1,10 @@ +import http.client + +# The product ID for the Pete-and-Gerry's organic large brown egges is 65902 +conn = http.client.HTTPSConnection("www.sayweee.com") +payload = '' +headers = {} +conn.request("GET", "/en/product/Pete-and-Gerry-s-Organic-Large-Brown-Eggs/65902?category=dairy02", payload, headers) +res = conn.getresponse() +data = res.read() +print(data.decode("utf-8")) \ No newline at end of file diff --git a/fp2_2.py b/fp2_2.py new file mode 100644 index 0000000..0f5dc63 --- /dev/null +++ b/fp2_2.py @@ -0,0 +1,32 @@ +import http.client +import xml.etree.ElementTree as ET + +conn = http.client.HTTPSConnection("www.sayweee.com") +payload = '' +headers = {} +conn.request("GET", "/en/product/Pete-and-Gerry-s-Organic-Large-Brown-Eggs/65902?category=dairy02", payload, headers) +res = conn.getresponse() +data = res.read() + +# Parse the HTML page content using ElementTree +root = ET.fromstring(data.decode('utf-8')) + +if root is not None: + # Find the script element with __NEXT_DATA__ data + next_data_element = None + for script_element in root.iter('script'): + if script_element.text is not None and '__NEXT_DATA__' in script_element.text: + next_data_element = script_element + break + + # Extract the __NEXT_DATA__ data + if next_data_element is not None: + next_data_text = next_data_element.txt + next_data_start_index = next_data_text.index('{') + next_data_end_index = next_data_text.rindex('}') + 1 + next_data_json = next_data_text[next_data_start_index:next_data_end_index] + print(next_data_json) + else: + print('__NEXT_DATA__ element not found') +else: + print('Failed to parse HTML content') diff --git a/fp2_3.py b/fp2_3.py new file mode 100644 index 0000000..4bc14d7 --- /dev/null +++ b/fp2_3.py @@ -0,0 +1,32 @@ +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +import json + +# Configure the Selenium driver options +options = Options() +options.add_argument('--headless') +options.add_argument('--disable-gpu') +driver = webdriver.Chrome(options=options) + +# Load the page and extract the __NEXT_DATA__ data +url = 'https://www.sayweee.com/en/product/Pete-and-Gerry-s-Organic-Large-Brown-Eggs/65902?category=dairy02' +driver.get(url) +wait = WebDriverWait(driver, 10) +next_data_element = wait.until(EC.presence_of_element_located((By.XPATH, '//script[contains(text(), "__NEXT_DATA__")]'))) +next_data_text = next_data_element.text +next_data_start_index = next_data_text.index('{') +next_data_end_index = next_data_text.rindex('}') + 1 +next_data_json = next_data_text[next_data_start_index:next_data_end_index] +next_data = json.loads(next_data_json) + +# Extract the price from the __NEXT_DATA__ data +price = next_data['props']['pageProps']['product']['price'] + +# Close the Selenium driver +driver.quit() + +# Print the price +print(price) diff --git a/fp2_4.py b/fp2_4.py new file mode 100644 index 0000000..c8334b6 --- /dev/null +++ b/fp2_4.py @@ -0,0 +1,36 @@ +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +import json + +# Configure the Selenium driver options +options = Options() +options.add_argument('--headless') +options.add_argument('--disable-gpu') +driver = webdriver.Chrome(options=options) + +# Load the page and extract the __NEXT_DATA__ data +url = 'https://www.sayweee.com/en/product/Pete-and-Gerry-s-Organic-Large-Brown-Eggs/65902?category=dairy02' +driver.get(url) +next_data_element = None +for script_element in driver.find_elements_by_xpath('//script'): + if '__NEXT_DATA__' in script_element.get_attribute('innerHTML'): + next_data_element = script_element + break + +if next_data_element is not None: + next_data_text = next_data_element.get_attribute('innerHTML') + next_data_start_index = next_data_text.index('{') + next_data_end_index = next_data_text.rindex('}') + 1 + next_data_json = next_data_text[next_data_start_index:next_data_end_index] + next_data = json.loads(next_data_json) + + # Extract the price from the __NEXT_DATA__ data + price = next_data['props']['pageProps']['product']['price'] + + # Print the price + print(price) +else: + print('__NEXT_DATA__ element not found') + +# Close the Selenium driver +driver.quit() diff --git a/hw12.py b/hw12.py new file mode 100644 index 0000000..d19ab2e --- /dev/null +++ b/hw12.py @@ -0,0 +1,7 @@ +i = 5 + +def f(arg=i): + print(arg) + +i = 6 +f() \ No newline at end of file