【问题标题】:how can I get html using python? [duplicate]如何使用 python 获取 html? [复制]
【发布时间】:2015-07-13 07:49:39
【问题描述】:

我想获取特定网站上的 html 文档。

这段代码运行良好。

import urllib2

link = "https://www.google.com"
print link
f = urllib2.urlopen(link)
myfile = f.read()
print myfile

但此代码不起作用。

import urllib2

link = "https://www.virustotal.com/en/file/7cf757e0943b0a6598795156c156cb90feb7d87d4a22c01044499c4e1619ac57/analysis/"
print link
f = urllib2.urlopen(link)
myfile = f.read()
print myfile

为什么不工作特定网站?

【问题讨论】:

  • 您是否收到错误或只是一个空文件?
  • 你怎么知道它不起作用?
  • 这不是 Python 问题,而是 VirusTotal 的一个有趣行为。即使使用curl -v https://www.virustotal.com/en/file/7cf757e0943b0a6598795156c156cb90feb7d87d4a22c01044499c4e1619ac57/analysis/ 也会返回Content-Length: 0

标签: python url https


【解决方案1】:

这很奇怪,我不知道为什么 urllib2 不起作用。

虽然我尝试使用 selenium 使用此代码并且它对我有用。

from selenium import webdriver
url = 'https://www.virustotal.com/en/file/7cf757e0943b0a6598795156c156cb90feb7d87d4a22c01044499c4e1619ac57/analysis/'
mydriver = webdriver.PhantomJS()
mydriver.get(url)
page = mydriver.page_source
print page.encode('utf-8')

如果你不了解 phantomjs,它只是一个无头浏览器。 您可以使用 FireFox 更改 phantomjs,它仍在工作

【讨论】:

  • 效果很好。我找到了另一种使用virustotal api的方法。谢谢。
猜你喜欢
  • 2011-05-28
  • 1970-01-01
  • 2023-01-20
  • 2011-11-09
  • 2019-10-24
  • 2011-08-04
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
相关资源
最近更新 更多