尝试从需要以单选按钮形式提供反馈响应的网站进行读取时出现HTTPError: Forbidden
错误。如何解决这个???
导致错误的示例url链接被注释掉:
url = 'http://www.londonstockexchange.com/exchange/news/market-news/market-news-detail/WWH/13673174.html'
以下是代码:
import urllib.request as req
import re
from bs4 import BeautifulSoup
# get all text from websites.
url = 'https://www.bbc.co.uk/sport/football'
#url = 'http://www.londonstockexchange.com/exchange/news/market-news/market-news-detail/WWH/13673174.html'
html = req.urlopen(url).read()
soup = BeautifulSoup(html)
# 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)
print(text)