0

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

美好的一天,我有以下代码,我想知道如何搜索匹配结果?我将尝试匹配两到三个单词。我尝试过 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()
4

1 回答 1

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为文档。

于 2013-09-18T08:00:14.403 回答