for question in questions: print(question.select_one(".question-hyperlink").getText()) print(question.select_one(".vote-count-post").getText())
{‘class’: [‘question-summary’], ‘id’: ‘question-summary-67532680’} question-summary-67532680 GKE Nginx Ingress Controller Oauth2 Proxy redirect 0 Workaround on nested async completion blocks from network calls? Without using PromiseKit 0 ax.text not being printed when using transform 0 Using html & javascript Populate textbox 0 How to pass JSON data through the Django backend to frontend view using Angular 0 Gitlab CI cannot pull private registry with DOCKER_AUTH_CONFIG 0 Shopify SKU Lookup using GraphQL 0
""" This module provides functions to convert a PDF to text. """
classConverter: """ A simple converter for converting PDFs to text. """
defconvert(self, path): """ Convert the given PDF to text. Parameters: path (str): The path to a PDF file. Returns: str: The content of the PDF file as text. """
path = Path(r"ecommerce/__init__.py") # create a path object print(path.exists()) # file exists or not print(path.is_file()) # is a file or not print(path.is_dir()) # is a directory or not print(path.name) print(path.stem) # return the file name without extension print(path.suffix) # return the extension print(path.parent) # parent folder
data = json.dumps(movies) Path("movies.json").write_text(data)
Demo 2: read JSON Files
1 2 3 4 5 6
import json from pathlib import Path
data = Path("movies.json").read_text() movies = json.loads(data) print(movies[0])
输出:
{‘id’: 1, ‘title’: ‘Terminator’, ‘year’: 1989}
Working with a SQLite Database
Demo 1: write
1 2 3 4 5 6 7 8 9 10 11
import sqlite3 import json from pathlib import Path
movies = json.loads(Path("movies.json").read_text())
with sqlite3.connect("db.sqlite3") as conn: command = "INSERT INTO Movies VALUES(?, ?, ?)" for movie in movies: conn.execute(command, tuple(movie.values())) conn.commit()
Demo 2: read
1 2 3 4 5 6 7
import sqlite3
with sqlite3.connect("db.sqlite3") as conn: command = "SELECT * FROM Movies" cursor = conn.execute(command) for row in cursor: print(row)
或:
1 2 3 4 5 6 7
import sqlite3
with sqlite3.connect("db.sqlite3") as conn: command = "SELECT * FROM Movies" cursor = conn.execute(command) movies = cursor.fetchall() print(movies)
Time
Working with Timestamps
1 2 3 4 5 6 7 8 9 10 11 12 13
import time
defsend_emails(): for i in range(1000000): pass
start = time.time() send_emails() end = time.time() duration = end - start print(duration)
from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from pathlib import Path import smtplib
with smtplib.SMTP(host="smtp.whu.edu.cn", port=25) as smtp: smtp.ehlo() # say hello to smtp server smtp.starttls() # TLS smtp.login("email_of_sender@email.com", "password") smtp.send_message(message) print("Sent...")
code1 = """ def calculate_xfactor(age): if age <= 0: raise ValueError("Age cannot be 0 or less.") return 10 / age try: calculate_xfactor(-1) except ValueError as error: pass """
code2 = """ def calculate_xfactor(age): if age <= 0: return None return 10 / age xfactor = calculate_xfactor(-1) if xfactor == None: pass """