【问题标题】:python 3.3 search for match in webpage resultspython 3.3在网页结果中搜索匹配
【发布时间】:2013-09-15 08:14:51
【问题描述】:

我的工作脚本的最新版本已包含在帖子底部。我正在研究如何在维基上做这个。 **

美好的一天,我有以下代码,我想知道如何在结果中搜索匹配项?我将尝试匹配两到三个单词。我尝试过 html2text、beautifulsoup、re.search 和其他几个。我是否没有实施我正确尝试过的事情,或者他们只是不工作。

import requests

s = requests.session()

url = 'http://company.name.com/donor/index.php'
values = {'username': '1234567',
          'password': '7654321'}

r = s.post(url, data=values)

# page which requires being logged in to view
url = "http://company.name.com/donor/donor.php"

# sending cookies as well
result = s.get(url)

我尝试了很多不同的方法,只是无法得到它。我想知道我需要使用哪个模块?我是否需要更改“结果”所在的数据形式?我没有尝试过的一件事是将“结果”写入文本文件。我想我可以这样做,然后在该文件中搜索我的匹配项......我只是在想有一个非常简单的方法可以做到这一点。

感谢任何帮助或指导

更新/编辑脚本:

## Script will, login, navigate to correct page, search and match, then print and text/sms result.

import re
import urllib
import smtplib
import requests
from bs4 import BeautifulSoup

s = requests.session()

url = 'http://company.name.com/donor/index.php'
values = {'username': '123456',
          'password': '654321'}

r = s.post(url, data=values)

# Now you have logged in
url = "http://company.name.com/donor/donor.php"

# sending cookies as well
result = s.get(url)

print (result.headers)
print (result.text)

result2 = (result.text)
match1 = re.findall('FindMe', result2);    #we are trying to find "FindMe" in "result2"

if len(match1) == 1:                       #if we find a match 
   matchresult = ('Yes it matched')
   print (matchresult)
else:                                      #if we don't find a match
   matchresult = ('Houston we have a problem')
   print (matchresult)

# send text from gmail account portion of code starts here.

body = matchresult

body = "" + body + ""

headers = ["From: " + 'Senders Name',
           "Subject: " + 'Type Subject Information',
           "To: " + '1234567890@mms.att.net',  #phone number and cell carrier @address
           "MIME-Version: 1.0",
           "Content-Type: text/html"]
headers = "\r\n".join(headers)

session = smtplib.SMTP('smtp.gmail.com', '587')

session.ehlo()
session.starttls()
session.ehlo
session.login('anemailaddress@gmail.com', 'passwordforemailaddress')

session.sendmail('senders name', '1234567890@mms.att.net', headers + "\r\n\r\n" + body)
session.quit()

【问题讨论】:

  • “匹配”是什么意思?请举例说明网站的内容、要匹配的模式以及预期的结果。
  • 对不起。我之前也发过类似的帖子,觉得问的太多了,所以我把它记下来了,并简化了问题。没有意识到我没有为此提供足够的信息。在 Windows 7 机器上使用 python 3.3。我访问的页面在源代码中有纯文本,我想搜索“恭喜”或“我们很抱歉”,然后有一个 if then 语句根据匹配的单词打印一些内容。希望有帮助。我已经完成了“if thens”。

标签: python string search module match


【解决方案1】:

仍然不确定我是否正确理解了这个问题,但根据您评论中的其他信息,这样做就足够了:

import urllib2
page = urllib2.urlopen("http://your.url.com")
content = page.read()
if "congratulations" in content:
    print ...
if "We're sorry" in content:
    print ...

当您正在寻找非常具体的单词时,无需使用正则表达式来匹配一些更通用的模式,也无需使用 HTML 解析器来查看文档的结构。看看这个字符串是不是in文档就行了。

【讨论】:

  • 感谢您的回复。我无法让它在登录后出现的网页上工作......这有意义吗?我认为这可能与cookie有关?加上我使用 python 3.x 所以我使用 urllib 和 re 模块。谢谢
  • 好的,我搞定了。只需将我的 urllib 相关代码放在正确的位置。感谢您的帮助! :) 我会向上箭头,但我至少需要 15 个代表 :) 现在我要使用 smtplib 和 gmail 发送电子邮件和我的搜索结果的文本......还有其他方式发送文本结果吗?