缓缓归来 发表于 2020-8-6 11:51:02

不报错不输出

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))


想请教一下大家,为什么运行出来不报错也没有输出结果啊

qiuyouzhi 发表于 2020-8-6 12:04:21

改成这样:

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))
页: [1]
查看完整版本: 不报错不输出