问题描述
我需要打开另一个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)
解决方法
这个问题太广泛了,无法直接回答,但是作为一般方法,您需要使用诸如Scrapy
或BeautifulSoup
之类的抓取库。
然后,您将在父URL(即http://dr-chuck.com/page1.htm)上执行GET
请求并解析该页面的HTML。然后,您将定位并提取href
HTML标记,并将URL本身保存到另一个变量中,然后对此执行另一个GET请求。