首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PythonAnywhere问题

PythonAnywhere问题
EN

Stack Overflow用户
提问于 2022-06-28 20:45:35
回答 1查看 115关注 0票数 0

我是一个新的python用户。当我试图在PythonAnywhere中运行我的代码时,我得到了以下错误,尽管它在我的本地PC上运行得很好。

代码语言:javascript
复制
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/zachfeatherstone/imputations.py", line 7, in <module>
    html = urlopen(url).read()
  File "/usr/local/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.9/urllib/request.py", line 517, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.9/urllib/request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/local/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.9/urllib/request.py", line 1389, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/usr/local/lib/python3.9/urllib/request.py", line 1349, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error Tunnel connection failed: 403 Forbidden>

它类似于:urllib.request.urlopen: ValueError:未知url类型

代码语言:javascript
复制
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import os.path

url = input("Enter the URL you want to analyse:  ")
html = urlopen(url).read()
soup = BeautifulSoup(html, features="html.parser")

# kill all script and style elements
for script in soup(["script", "style"]):
    script.extract()    # rip it out

# get text
text = soup.get_text()

# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
wordList = text.split()
 
#imputations
imputations = []

if "unprofessional" in wordList:
    unprofessional_imputation = "That our Client is unprofessional."
    imputations.append(unprofessional_imputation)
  
print(imputations)
#print(wordList)
#print(text)

#saving file
save_path = 'C:/Users/team/downloads'
name_of_file = input("What do you want to save the file as?   ")
completeName = os.path.join(save_path, name_of_file+".txt")         
f = open(completeName, "w")

# traverse paragraphs from soup
for words in imputations:
   f.write(words)
   f.write("\n")

我很抱歉,如果这个问题已经得到了回答。,我如何在PythonAnywhere中运行它,这样我就可以在网络上进行部署了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-28 21:42:40

随着您的请求发送标题信息可能会有所帮助。您可以向请求中传递一个请求对象,如下所示:

代码语言:javascript
复制
url = input("Enter the URL you want to analyse:  ")

header = {'User-Agent': 'Gandalf'}
req = urllib.request.Request(url, None, header)
html = urllib.request.urlopen(req)
html = html.read()

soup = BeautifulSoup(html, features="html.parser")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72792960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档