gpt4 book ai didi

python - 我怎样才能让 tormysql 做插入?

转载 作者:行者123 更新时间:2023-11-29 03:18:26 24 4
gpt4 key购买 nike

我认为我对这里的控制流的理解是有缺陷的。我想要的是抓取一堆 URL,当我遇到它们时,从池中启动一个连接以将 URL 插入我的数据库。

我认为这里的问题是我对 yield 的理解。如何让 tormysql 接受参数并异步使用连接将该参数写入数据库?

基本上,我只希望 insert_to_db() 异步插入一个 URL 到我的数据库。但是 insert_to_db() 实际上并没有运行。

from lxml import html
import tormysql
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
import time
import sys
import csv
from shutil import copyfile

import requests
from bs4 import BeautifulSoup



pool = tormysql.ConnectionPool(
max_connections = 20, #max open connections
idle_seconds = 7200, #conntion idle timeout time, 0 is not timeout
wait_connection_timeout = 3, #wait connection timeout
host = "unidb.cfavdkskfrrc.us-west-2.rds.amazonaws.com",
user = "#",
passwd = "#",
db = "diplomacy_data",
charset = "utf8"
)

def insert_to_db(game_url):
print(game_url)
with (yield pool.Connection()) as conn:
try:
with conn.cursor() as cursor:
yield cursor.execute("INSERT INTO FvA_urls(URL) VALUES('%s')" % game_url)
except:
yield conn.rollback()
else:
print('committed', game_url)
yield conn.commit()


username = "#"
password = "#"
login_url = "https://webdiplomacy.net/logon.php"

driver = webdriver.Chrome()
driver.get(login_url)
driver.find_element_by_css_selector('body > div.content.content-follow-on > form > ul > li:nth-child(2) > input[type="text"]').send_keys(username)
driver.find_element_by_css_selector('body > div.content.content-follow-on > form > ul > li:nth-child(5) > input[type="password"]').send_keys(password)
driver.find_element_by_css_selector('body > div.content.content-follow-on > form > ul > li:nth-child(10) > input').click()

url = "https://webdiplomacy.net/gamelistings.php?"
params = "page-games=1&gamelistType=Finished&searchOn=on"
driver.get(url + params)
driver.find_element_by_css_selector('body > div:nth-child(5) > div:nth-child(2) > form > li:nth-child(5) > input[type="radio"]:nth-child(6)').click()
driver.find_element_by_css_selector('body > div:nth-child(5) > div:nth-child(2) > form > input').click()
# Get all the URLS
page_num = 0
while True:
page_num += 1
if page_num % 20 == 0:
print(page_num)
a = driver.find_elements(By.XPATH, '/html/body/div[5]/div[3]/div[*]/div[6]/div[2]/a')
if len(a) < 1:
pool.close()
exit()
else:
for button in a:
game_url = button.get_attribute('href')
insert_to_db(game_url)
driver.find_element_by_css_selector('body > div:nth-child(6) > div:nth-child(4) > div:nth-child(1) > div:nth-child(2) > a:nth-child(3) > img').click()

我在任何地方都能找到的所有 TorMySQL 用法示例都只是将 1 重复异步插入到表中。显然完全没用。

难道不可能让每个连接都独立做某事吗?比如这个库是否只对异步多次执行完全相同的事情有用?

最佳答案

我相信用于字符串插值的“%”语法是一种被证明容易受到 SQL 注入(inject)攻击的模式。任何合并到 SQL 文本中的潜在不安全值都必须正确转义。

我还认为包含单引号会导致生成无效的 SQL 语法,并且语句的执行会因语法错误而失败

 cursor.execute("INSERT INTO FvA_urls(URL) VALUES('%s')" % game_url)
^ ^ ^

(我可能找错树了。我还没有通过测试验证这一点。也许单引号是必需的或可以容忍的,也许值被正确地转义了。)

我认为更好的模式是使用逗号代替百分号,并将元组作为参数传递,如下所示:

 cursor.execute("INSERT INTO FvA_urls(URL) VALUES( %s )", (game_url,))
^ ^ ^ ^ ^^

为了比较,看一下example/pool.py中的这行代码

    cur = yield POOL.execute("SELECT SLEEP(%s)", (t,))
^ ^ ^^

我敢打赌这就是问题所在。

但本着 StackOverflow 作为问答网站的精神,我认识到还提出了这些问题:

问:让每个连接独立做某事是不可能的吗?

问:这个库是否只对异步多次执行完全相同的事情有用?

example/pool.py 中的代码是否回答了您的问题?正在使用不同的值执行相同的 SQL 语句,这似乎是您试图完成的。

关于python - 我怎样才能让 tormysql 做插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50205725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com