簡體   English   中英

如何在 MySQL 中查找包含任意數量的雙引號和單引號的文本

[英]How to find text in MySQL containing arbitrary number of double and single quotations

我正在抓取一個網站,該網站返回包含單引號和雙引號的 html,示例文本是

<div class="article__content">                                    <font face="Arial Helvetica sans-serif" size="3">Successful hires will expand the group's ongoing efforts applying machine learning to drug discovery biomolecular simulation and biophysics.  Ideal candidates will have demonstrated expertise in developing deep learning techniques as well as strong Python programming skills.  Relevant areas of experience might include molecular dynamics structural biology medicinal chemistry cheminformatics and/or quantum chemistry but specific knowledge of any of these areas is less critical than intellectual curiosity versatility and a track record of achievement and innovation in the field of machine learning.</font>                                </div>

當我在 phpmyadmin 中編寫以下查詢時:

SELECT COUNT(*) FROM scrappedjobs WHERE JobDescription = '"<div class="article__content">                                    <font face="Arial Helvetica sans-serif" size="3">Successful hires will expand the group's ongoing efforts applying machine learning to drug discovery biomolecular simulation and biophysics.  Ideal candidates will have demonstrated expertise in developing deep learning techniques as well as strong Python programming skills.  Relevant areas of experience might include molecular dynamics structural biology medicinal chemistry cheminformatics and/or quantum chemistry but specific knowledge of any of these areas is less critical than intellectual curiosity versatility and a track record of achievement and innovation in the field of machine learning.</font>                                </div>"'

當它存在於數據庫中時,我收到錯誤或計數 = 0。 請告訴我如何處理抓取數據中包含引號的字符串。 我是新手,我找到的所有答案都是針對 php 而不是 python

編輯:

python is 代碼如下:

self.Cursor = self.db.cursor(buffered=True)
    FetchQuery = "SELECT COUNT(*) FROM scrappedjobs where URL = %s AND JobDescription = %s"
    self.Cursor.execute(FetchQuery,("\'" + item['url'] + "\'", item['text']))

    if(self.Cursor.fetchone()[0]== 0): #If the url does not exist in database
        print("Inserting into db...\n")
        InsertQuery = "INSERT INTO scrappedjobs (URL, JobTitle, JobDescription, CompanyName) VALUES (%s, %s, %s, %s)"
        self.Cursor.execute(InsertQuery,(item['url'], item['title'], item['text'], item['companyName']))
        self.db.commit()

基本上 if 條件沒有觸發,盡管數據庫中有數據。

你需要這樣的東西:

  create table #scrappedjobs
    (
       JobDescription NVARCHAR(1000)
    )
    
    insert into #scrappedjobs (JobDescription)
    VALUES('"<div class="article__content">"')
    
    select * from #scrappedjobs
    
    SELECT COUNT(*) FROM #scrappedjobs WHERE JobDescription = '"<div class="article__content">"'

-- Second select with like :
    SELECT COUNT(*) FROM #scrappedjobs WHERE JobDescription like '%"<div class="article__content">"%' 

請記住,您需要在 JobDescription 值的開頭和結尾使用 '。

您的示例字符串以這樣的方式開頭: Successful hires will expand the group's . group's單引號將被 MySQL 解釋為 SELECT 語句中字符串條件的結束。 為了實現這一點,您必須在將文本存儲在數據庫中時將每個'替換為'' ,並在需要時將其反轉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM