【问题标题】:python 3 open and read url without url namepython 3打开并读取没有url名称的url
【发布时间】:2014-11-11 18:47:17
【问题描述】:

我已经浏览了相关问题,但我没有找到这个问题的答案:

我想打开一个 url 并解析它的内容。

当我在 google.com 上这样做时,没问题。

当我在没有文件名的 url 上执行此操作时,我经常被告知我读取了一个空字符串。

以下代码为例:

import urllib.request

#urls = ["http://www.google.com", "http://www.whoscored.com", "http://www.whoscored.com/LiveScores"]
#urls = ["http://www.whoscored.com", "http://www.whoscored.com/LiveScores"]
urls = ["http://www.whoscored.com/LiveScores"]
print("Type of urls: {0}.".format(str(type(urls))))
for url in urls:
    print("\n\n\n\n---------------------------------------------\n\nUrl is: {0}.".format(url))
    sock=urllib.request.urlopen(url)
    print("I have this sock: {0}.".format(sock))
    htmlSource = sock.read()
    print("I read the source code...")
    htmlSourceLine = sock.readlines()
    sock.close()
    htmlSourceString = str(htmlSource)
    print("\n\nType of htmlSourceString: " + str(type(htmlSourceString)))
    htmlSourceString = htmlSourceString.replace(">", ">\n")
    htmlSourceString = htmlSourceString.replace("\\r\\n", "\n")
    print(htmlSourceString)
    print("\n\nI am done with this url: {0}.".format(url))

我不知道我有时会得到那个空字符串作为没有文件名的 url 的返回值——例如示例中的“www.whoscored.com/LiveScores”——而“google .com”或“www.whoscored.com”似乎一直有效。

我希望我的表述是可以理解的......

【问题讨论】:

  • Python 中有一些库,比如 BeautifulSoup 来解析内容。我不确定你想做什么

标签: python python-3.x web-scraping


【解决方案1】:

看起来该网站已被编码为明确拒绝来自非浏览器客户端的请求。您将不得不欺骗创建会话等,确保 Cookie 根据需要来回传递。第三方 requests 库可以帮助您完成这些任务,但最重要的是您必须了解更多有关该网站如何运作的信息。

【讨论】:

  • 谢谢你的回答,我想这是我为这篇文章得到的第一个答案的简短版本;+> 一个网站有这么多的操作方式吗?您能否详细说明这一点和/或指出一个可以帮助我了解它的地方,谢谢!
  • 好吧,我想automate 包将是一个不错的起点。但要小心 - 还有许多其他 hHTP 要求。
  • 你可以看到我是一个新手,什么是自动化包,我在哪里可以找到它,我能用它做什么!
  • 不足为奇,因为我现在发现我的包名弄错了——我的意思是mechanize。对不起。
【解决方案2】:

您的代码间歇性地为我工作,但使用 requests 并发送用户代理工作正常:

headers = {
    'User-agent': 'Mozilla/5.0,(X11; U; Linux i686; en-GB; rv:1.9.0.1): Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'}
urls = ["http://www.whoscored.com/LiveScores"]
import requests

print("Type of urls: {0}.".format(str(type(urls))))
for url in urls:
    print("\n\n\n\n---------------------------------------------\n\nUrl is: {0}.".format(url))
    sock= requests.get(url, headers=headers)
    print("I have this sock: {0}.".format(sock))
    htmlSource = sock.content
    print("I read the source code...")
    htmlSourceString = str(htmlSource)
    print("\n\nType of htmlSourceString: " + str(type(htmlSourceString)))
    htmlSourceString = htmlSourceString.replace(">", ">\n")
    htmlSourceString = htmlSourceString.replace("\\r\\n", "\n")
    print(htmlSourceString)
    print("\n\nI am done with this url: {0}.".format(url))

【讨论】:

  • 感谢您的回答。既然你提到了请求,是的,我在这里的另一篇文章中看到了一些关于它的东西,如果你能容忍我的话,这给我带来了另一个问题:我试图安装请求,但显然它不起作用,因为我仍然获取“没有名为请求的模块”消息。我能做些什么呢?能不能提一下我前段时间安装了anaconda,从安装过程中可以看出anaconda接管了python34。我希望你能给我一些好的想法,这样我就可以继续下去了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多