我正在抓取一个网站,它返回包含单引号和双引号的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条件不会触发,尽管数据在数据库中。
发布于 2021-07-29 15:29:06
示例字符串的开头是这样的:Successful hires will expand the group's。MySQL会将group's中的单引号解释为SELECT语句中字符串条件的结束。为了实现这一点,在数据库中存储文本并在需要时反转时,必须用''替换每个'。
发布于 2021-07-29 15:16:33
你需要这样的东西:
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值的开始和结束位置使用‘。
https://stackoverflow.com/questions/68571409
复制相似问题