|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import urllib.request
- import re
- def open_url(url):
- req=urllib.request.Request(url)
- req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36')
- page=urllib.request.urlopen(req)
- html=page.read().decode('utf-8','ignore')
- return html
- def get_img(html):
- p=r'<img alt="赵丽颖" src="[^"]+\.jpg"'
- imglist=re.findall(p,html)
- for each in imglist:
- print(each)
- if __name__=='__main__':
- url="http://www.ik123.com/q/tuku/fzl/61045.html"
- get_img(open_url(url))
复制代码
想请教一下大家,为什么运行出来不报错也没有输出结果啊
改成这样:
- from requests import get
- import re
- def open_url(url):
- headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}
- html = get(url, headers = headers)
- html.encoding = html.apparent_encoding
- return html.text
- def get_img(html):
- p=r'<img alt="赵丽颖" src="[^"]+\.jpg"'
- imglist=re.findall(p,html)
- for each in imglist:
- print(each)
- if __name__=='__main__':
- url="http://www.ik123.com/q/tuku/fzl/61045.html"
- get_img(open_url(url))
复制代码
|
|