gpt4 book ai didi

python - 如何使用 python 从 MySql 表循环解析模式

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

我正在尝试用正则表达式解析scrapy结果,问题是我的正则表达式模式位于MySQL表中..我很难按顺序循环模式以便返回没有任何html标签的干净内容..简单地说 html 结果 scrapy -> 用 row1 中的模式解析(例如:内容上方的干净 html),用 row2 中的模式解析(内容下方的干净 html),..... -> clean

示例

<body>
<title>
<some tags>
<content>
<footer tags>
<another tags>
</body>

我正在尝试使用此表、字段名称(模式、序列、替换器)、值来清理该 html:

row1    <body.*?some tags>  1  None
row2 <footer.*?/body> 2 None
row3 <br> 3 Enter
row4 #&quot 4 ""

所以我有一个干净的内容作为返回,我使用正则表达式替换模式,而不是 xpath 匹配,因为我期望用自己的 html 标签变体废弃大量网页

这是我的代码,它没有引发错误,但结果重复了..它应该是来自 1 个 scrapy 结果的 1 个干净结果。我认为我做错了什么,但无法弄清楚,因为我是 python 和 scrapy 的新手

   def parse(self, response):
for mbuh in response.xpath('//body'):
Item = ParsingerbotItem()
Item['ling'] = str(response.url)
ngaliase = re.findall("\w+.com", str(response.url))[0]
mmhtml = mbuh.xpath('//body').extract()
cur.execute("select aliase, pattern, seq, opsi, replacer from tb_bersihin where aliase='"+ngaliase+"\' order by seq asc")
for filde in cur.fetchall():
faliase = filde[0]
fpattern = filde[1]
fseq = filde[2]
fopsi = filde[3]
freplacer = filde[4]
print "faliase=%s,fpattern=%s,furutan=%d,fopsi=%s,freplacer=%s" % \
(faliase, fpattern, fseq, fopsi, freplacer )
if ( freplacer == "NO" ) : freplacer=""
if ( fopsi == "NL" ) : fopsi="re.DOTALL"
k1 = re.sub(fpattern , freplacer, str(mmhtml), re.DOTALL)
print k1

提前谢谢

最佳答案

@insecte,看看this漂亮的小蜘蛛。上面写着this CSV 文件并从页面进行通用解析。以此为起点,用数据库读数更改 CSV 文件读数。您很可能不会拥有 1000 个 URL,因此只需从数据库中读取一次并将 XPath 表达式存储在内存中即可。有帮助吗?

关于python - 如何使用 python 从 MySql 表循环解析模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37692683/

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