gpt4 book ai didi

Python 脚本有效但编译后失败 (Windows)

转载 作者:行者123 更新时间:2023-12-04 16:22:50 25 4
gpt4 key购买 nike

我正在编写一个脚本来抓取网站,问题是当我使用解释器运行它时它可以正常工作,但是在编译它(PyInstaller 或 Py2exe)之后它失败了,似乎是 Mechanize/请求都无法保持 session 活着。

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

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 脚本上正常工作并编译,在寡妇上作为脚本工作但在编译时失败。

最佳答案

py2exe教程提到你需要MSVCR90.dll,你有没有检查它在PC上的存在?

关于Python 脚本有效但编译后失败 (Windows),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20819647/

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