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

标签 python mysql regex scrapy

我正在尝试用正则表达式解析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/

相关文章:

python - 如何让它将张量板日志附加到以前的运行中?

python - 如何在python中拆分字符串

java - 如何解决hibernate建表时提示创建表不成功的问题

php - 我可以在 PHP 中混合使用 MySQL API 吗?

php - str_replace + 正则表达式

c# - 结合正面前瞻和负面前瞻?

python - Python 上的 %precision 2

mysql - 无法执行数据库迁移: org. sonar.db.version.v60.PopulateProfileKeyOfActivities

regex - 正则表达式帮助-逗号分隔的字符串

python - 如何使循环运行特定次数