如何在 MySQL 中查找包含任意数量的双引号和单引号的文本
How to find text in MySQL containing arbitrary number of double and single quotations
我正在抓取一个 return 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代码如下:
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">"%'
请记住,您需要在职位描述值的开头和结尾使用 '。
您的示例字符串开头如下:Successful hires will expand the group's
。 group's
中的单引号将被 MySQL 解释为 SELECT 语句中字符串条件的结束。
为了实现这一点,在将文本存储在数据库中并在需要时反转它时,您必须将每个 '
替换为 ''
。
我正在抓取一个 return 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代码如下:
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">"%'
请记住,您需要在职位描述值的开头和结尾使用 '。
您的示例字符串开头如下:Successful hires will expand the group's
。 group's
中的单引号将被 MySQL 解释为 SELECT 语句中字符串条件的结束。
为了实现这一点,在将文本存储在数据库中并在需要时反转它时,您必须将每个 '
替换为 ''
。