如何使用Python在另一个URL中打开一个URL?

问题描述

我需要打开另一个URL内的URL,但是我不知道该怎么做

这是我写的代码(对初学者而言),它向我发送了回溯:URLError:

请帮助我

谢谢你

import re 
import urllib.request,urllib.parse,urllib.error

fhand=urllib.request.urlopen("http://dr-chuck.com/page1.htm")
for line in fhand:
    words=line.decode().strip()
    if re.search("href=",words):
        print(words)
        w=words.split()
        print(w)
        i=w[1]
        print(i)
        p=i[5:40]
        print(p)
        q=urllib.request.urlopen(p)
        print(q)

解决方法

这个问题太广泛了,无法直接回答,但是作为一般方法,您需要使用诸如ScrapyBeautifulSoup之类的抓取库。

然后,您将在父URL(即http://dr-chuck.com/page1.htm)上执行GET请求并解析该页面的HTML。然后,您将定位并提取href HTML标记,并将URL本身保存到另一个变量中,然后对此执行另一个GET请求。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...