forked from SVIG/python-bootcamp
final project update
This commit is contained in:
@@ -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])
|
||||||
@@ -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"))
|
||||||
@@ -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')
|
||||||
@@ -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)
|
||||||
@@ -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()
|
||||||
Reference in New Issue
Block a user