鱼C论坛

 找回密码
 立即注册
查看: 5998|回复: 5

urlopen error no host given怎么办

[复制链接]
发表于 2018-6-29 15:09:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import urllib.request
import re
import chardet


def get_html(url):
    html = urllib.request.urlopen(url).read()
    x = chardet.detect(html)['encoding']
    html = html.decode(x)
    return html

url = 'https://fishc.taobao.com'
html = get_html(url)
img_address = re.findall(r'<img src="([^"]+)"', html)[1]

print(img_address)


x = get_html('http://' + img_address)
'''

for i in img_address:
    x = 1
    urllib.request.urlretrieve('http://' + i, '%d.jpg'%x)
    x += 1

'''
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-6-29 15:16:33 | 显示全部楼层
除了第三方的requests模块,其他的urllib之类使用上完全是反人类设计
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-29 15:17:14 | 显示全部楼层
你正则表达式匹配到的数据是类似这样的:
  1. //img.alicdn.com/bao/uploaded/i3/91760992/TB2fN5cdbBmpuFjSZFAXXaQ0pXa_!!91760992.png_120x120.jpg
复制代码

所以你第二次调用get_html函数传入的实参应该是这样的:
  1. x = get_html('http:' + img_address)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-24 22:26:31 | 显示全部楼层
我也遇到这个问题了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-24 22:38:35 | 显示全部楼层
你这只到资源,写了个协议,没有主机
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-25 00:33:17 | 显示全部楼层
本帖最后由 XiaoPaiShen 于 2019-10-25 00:34 编辑

我用你的代码无法decode html
我就使用了自动下载设置代理

首先把 【爬虫福利】自动下载设置代理 中的代码保存下来,取文件名为proxypool.py

  1. import proxypool as pool

  2. import urllib.parse
  3. import re

  4. ##def get_html(url):
  5. ##    html = urllib.request.urlopen(url).read()
  6. ##    x = chardet.detect(html)['encoding']
  7. ##    html = html.decode(x)
  8. ##    return html

  9. proxy = pool.ProxyPool()
  10. url = 'https://fishc.taobao.com'

  11. html = proxy.read_url(url, decode='text')
  12. # html = get_html(url)

  13. img_address = re.findall(r'<img src="([^"]+)"', html)[1]
  14. # print(img_address)

  15. img_url = urllib.parse.urljoin('http:', img_address)
  16. print(img_url)


  17. # x = get_html('http://' + img_address)
  18. '''

  19. for i in img_address:
  20.     x = 1
  21.     urllib.request.urlretrieve('http://' + i, '%d.jpg'%x)
  22.     x += 1
  23. '''
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-31 12:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表