【发布时间】:2015-08-03 18:50:31
【问题描述】:
我正在尝试避免这种情况 网址 = 'http://www.jmlr.org/proceedings/papers/v36/li14.pdf 网址。这是我的代码
html = requests.get(url)
htmlText = html.text
soup = BeautifulSoup(htmlText)
print soup #gives garbage
但是它给出了我认为是垃圾的奇怪符号。这是一个 html 文件,所以它不应该尝试将其解析为 pdf 文件吗?
我尝试了以下操作: How to correctly parse UTF-8 encoded HTML to Unicode strings with BeautifulSoup?
request = urllib2.Request(url)
request.add_header('Accept-Encoding', 'utf-8') #tried with 'latin-1'too
response = urllib2.urlopen(request)
soup = BeautifulSoup(response.read().decode('utf-8', 'ignore'))
还有这个: Python and BeautifulSoup encoding issues
html = requests.get(url)
htmlText = html.text
soup = BeautifulSoup(htmlText)
print soup.prettify('utf-8')
两者都给了我垃圾,即没有正确解析 html 标签。尽管元字符集是“utf8”,最后一个链接还建议编码可能与我不同,所以我也尝试了上面的“latin-1”但似乎没有任何效果
关于如何抓取给定链接以获取数据的任何建议?请不要建议在文件上下载和使用 pdfminer。欢迎询问更多信息!
【问题讨论】:
标签: python html pdf utf-8 beautifulsoup