Python脚本工作但编译后失败(Windows)

时间:2013-12-28 21:36:17

标签: python compilation web-scraping mechanize python-requests

我正在编写一个脚本来抓取一个网站,问题是当我用解释器运行它时它能正常工作,但是在编译它之后(PyInstaller或Py2exe)它失败了,它似乎是机械化/请求两者无法使会话保持活跃状态​​。

我在这里隐藏了我的用户名和密码,但我确实将它们正确地放在了已编译的代码中

import requests
from bs4 import BeautifulSoup as bs
from sys import argv
import re
import logging

url = argv[1]
payload = {"userName": "real_username", "password": "realpassword"}
session = requests.session()
resp = session.post("http://website.net/login.do", data=payload)
if "forgot" in resp.content:
    logging.error("Login failed")
    exit()

resp = session.get(url)
soup = bs(resp.content)
urlM = url[:url.find("?") + 1] + "page=(PLACEHOLDER)&" + \
url[url.find("?") + 1:]
# Get number of pages
regex = re.compile("\|.*\|\sof\s(\d+)")
script = str(soup.findAll("script")[1])
epNum = int(re.findall(regex, script)[0])  # Number of EPs
pagesNum = epNum // 50
links = []
# Get list of links
# If number of EPs > 50, more than one page
if pagesNum == 0:
    links = [url]
else:
    for i in range(1, pagesNum + 2):
        url = urlM.replace("(PLACEHOLDER)", str(i))
        links.append(url)
# Loop over the links and extract info: ID, NAME, START_DATE, END_DATE
raw_info = []
for pos, link in enumerate(links):
    print "Processing page %d" % (pos + 1)
    sp = bs(session.get(link).content)
    table = sp.table.table
    raw_info.extend(table.findAll("td"))
epURL = "http://www.website.net/exchange/viewep.do?operation"\
"=executeAction&epId="
# Final data extraction
raw_info = map(str, raw_info)
ids = [re.findall("\d+", i)[0] for i in raw_info[::4]]
names = [re.findall("<td>(.*)</td", i)[0] for i in raw_info[1::4]]
start_dates = [re.findall("<td>(.*)</td", i)[0] for i in raw_info[2::4]]
end_dates = [re.findall("<td>(.*)</td", i)[0] for i in raw_info[3::4]]
emails = []
eplinks = [epURL + str(i) for i in ids]
print names

错误发生在epNum变量的级别上,这意味着我认为HTML页面不是我请求的那个,但它在linux脚本上正常工作并编译,在脚本上作为脚本工作但在编译时失败。 / p>

1 个答案:

答案 0 :(得分:0)

py2exe教程提到您需要MSVCR90.dll,您是否在PC上查看了它的内容?